首页 > 开发 > AJAX > 正文

AJAX教程(16):通过XMLHTTP进行一次指定的HEAD请求

2024-09-01 08:26:07
字体:
来源:转载
供稿:网友

通过xmlhttp进行一次指定的head请求:

<html>
<head>
<script type="text/javascript">
var xmlhttp;
function loadxmldoc(url)
{
xmlhttp=null;
if (window.xmlhttprequest)
  {// all modern browsers
  xmlhttp=new xmlhttprequest();
  }
else if (window.activexobject)
  {// for ie5, ie6
  xmlhttp=new activexobject("microsoft.xmlhttp");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=state_change;
  xmlhttp.open("get",url,true);
  xmlhttp.send(null);
  }
else
  {
  alert("your browser does not support xmlhttp.");
  }
}

function state_change()
{
if (xmlhttp.readystate==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "ok"
    document.getelementbyid('p1').innerhtml="this file was last modified on: " + xmlhttp.getresponseheader('last-modified');
    }
  else
    {
    alert("problem retrieving data:" + xmlhttp.statustext);
    }
  }
}
</script>
</head>
<body>

<p id="p1">
the getresponseheader() function returns a header from a resource.
headers contain file information like length,
server-type, content-type, date-modified, etc.</p>

<button onclick="loadxmldoc('/example/ajax/test_xmlhttp.txt')">get "last-modified"</button>

</body>
</html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表