xml getResponseHeaders()函数

前端之家收集整理的这篇文章主要介绍了xml getResponseHeaders()函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

getResponseHeaders():获取响应的所有http头;

注:每个http头名称和值用冒号分割,并以\r\n结束。当send方法完成后才可调用方法

在此为了获取响应的所有的http头,我写了一个简单的demo,如下:

index.html

<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Strict//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
<html>
<head>
<Meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>insert into title</title>
<script type="text/javascript" src="ajax.js"></script>
</head>
<body>
     <p id="show">show</p>
     <button onclick="loadtxtDoc('test.txt')">Get XML HTTP header</button>
</body>
</html>

test.txt

this is test using XML Request to show a text file;

ajax.js

var xmlHttp;s

function getXmlHttpObject(){
    var xmlObject=null;
    if(window.XMLHttpRequest){
        xmlObject=new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xmlObject=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return xmlObject;
}


function loadtxtDoc(url){
    xmlHttp=getXmlHttpObject()
    if(xmlHttp!=null){
        xmlHttp.onreadystatechange=stateChange;
        xmlHttp.open("GET",url,true);
        xmlHttp.send(null);
    }else{
        alert("Browser does not support HTTP Request");
    }
}

function stateChange(){
    if(xmlHttp.readyState==4){
        if(xmlHttp.status==200){
            document.getElementById("show").innerHTML=xmlHttp.getAllResponseHeaders();
        }else{
            alert("Prolem retrieving XML data"+xmlHttp.statusText);
        }
    }
}

Output:

ETag: "4000000030305-3a-522c7fa0e3668" Content-Length: 58 Keep-Alive: timeout=5,max=98 Content-Type: text/plain Last-Modified: Fri,23 Oct 2015 16:19:32 GMT
原文链接:https://www.f2er.com/xml/295961.html

猜你在找的XML相关文章