ajax对象getAllResponseHeaders方法

  ajax对象getAllResponseHeaders()方法用于获取所有HTTP头信息,在获取时只用HEAD即可获取到。例如,需要获取全部的HTTP响应头信息,其实现方法如代码所示。

代码 获取全部响应头信息

+展开
-HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>获取HTTP头信息</title>
<script type="text/javascript">
var xmlHttp=false;
//创建XMLHttpRequest对象
function createXMLHttpRequest() {
if(window.ActiveXObject) { //IE
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
catch(e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
catch(e) {
window.alert("创建XMLHttpRequest对象错误"+e);
}
}
else if(window.XMLHttpRequest) { //非IE
xmlHttp = new XMLHttpRequest();
}
if(!(xmlHttp)) {
window.alert("创建XMLHttpRequest异常!");
}
}

function HeadRequest() {
createXMLHttpRequest();
xmlHttp.onreadystatechange = getHeadInfo;
xmlHttp.open("HEAD""http://www.rzchina.net"false);
xmlHttp.send(null);
}

function getHeadInfo() {
if(xmlHttp.readyState == 4) {
 alert(xmlHttp.getAllResponseHeaders());
}
}
</script> 
</head>
<body>
<center>
<h1>获取HTTP头信息</h1>
<input type="button" value="GetAllResponseHeaders" onclick="HeadRequest()">
</center>
</body>
</html>


代码18.2的运行后得到结果如下

Cache-Control: private
Content-Length: 55100
Content-Type: text/html
Date: Thu, 13 Mar 2008 16:48:03 GMT

加支付宝好友偷能量挖...


原创文章,转载请注明出处:ajax对象getAllResponseHeaders方法

评论(0)Web开发网
阅读(231)喜欢(0)JavaScript/Ajax开发技巧