在js中获取响应头的信息

主要是XMLHttpRequest对象的使用,可查看W3Cshool
<script type="text/javascript" language="javascript">
var xmlHttp = createXMLHttpRequest();
xmlHttp.onreadystatechange=state_Change;
xmlHttp.open("HEAD", window.location.href, true);//请求的url必须在同一个域,即js不允许跨域访问,第三个参数为true表示是异步请求,不会阻塞后续js代码的执行,此时就需要设定一个回调函数,如上一行
xmlHttp.send(null);

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        return new ActiveXObject("Microsoft.XMLHTTP");//如果是IE5 IE6
    }
    else if (window.XMLHttpRequest) {//其他浏览器
        return new XMLHttpRequest();
    }
}
function state_Change()
{
      if (xmlHttp.readyState==4)
      {// 4 = "loaded"
            //alert(xmlHttp.status);
              if (xmlHttp.status==200)
            {// 200 = "OK"
                  var dateStr = xmlHttp.getResponseHeader('Date');
                  var now = new Date(dateStr);//alert(now);alert("ddf");
                  total_seconds=(endDate.getTime()-now.getTime())/1000;
                  setInterval('exe()',1000);
            }
              else
            {
                alert("Problem retrieving data:" + xmlHttp.statusText);
            }
      }
}
</script>
posted @ 2012-02-09 09:00  SEC.VIP_网络安全服务  阅读(464)  评论(0编辑  收藏  举报