Jquery 无法使用的XMLHttpRequest.getAllResponseHeaders()读取响应标头http://www.google.com

Jquery 无法使用的XMLHttpRequest.getAllResponseHeaders()读取响应标头http://www.google.com,jquery,httpresponse,cors,Jquery,Httpresponse,Cors,我在标题中使用了CORS(跨源资源共享),以允许客户端连接到其他web资源。我已尝试连接到,但无法获取标题的任何信息。我需要从标题中读取日期 这是我从中使用的代码 函数getCORS(url、数据、回调、类型){ 试一试{ //尝试使用jQuery获取数据 get(url、数据、回调、类型); //Tr get(url、数据、函数(数据、textStatus、jqxhr){ 警报(“成功”+数据+”文本状态:“+textStatus+”--“+jqxhr.getAllResponseHeade

我在标题中使用了CORS(跨源资源共享),以允许客户端连接到其他web资源。我已尝试连接到,但无法获取标题的任何信息。我需要从标题中读取日期

这是我从中使用的代码


函数getCORS(url、数据、回调、类型){
试一试{
//尝试使用jQuery获取数据
get(url、数据、回调、类型);
//Tr
get(url、数据、函数(数据、textStatus、jqxhr){
警报(“成功”+数据+”文本状态:“+textStatus+”--“+jqxhr.getAllResponseHeaders());
});
}捕获(e){
//jQuery get()失败,请尝试IE8 CORS或使用代理
if(jQuery.browser.msie&&window.XDomainRequest){
//使用Microsoft XDR
var xdr=新的XDomainRequest();
xdr.open(“get”,url);
xdr.onload=函数(){
回调(handleXDROnload(this,type),'success',this);
};
xdr.onreadystatechange()=函数()
{
警报(xdr.getAllResponseHeaders());
}
xdr.send();
}否则{
试一试{
//古老的浏览器,使用我们的代理
var mycallback=函数(){
var textstatus='错误';
var数据='错误';
if((this.readyState==4)
&&(this.status=='200')){
textstatus=‘成功’;
数据=this.responseText;
}
回调(数据、文本状态);
};
//proxy_xmlhttp是您必须设置的单独脚本
request=newproxy_xmlhttp();
打开('GET',url,true);
request.onreadystatechange=mycallback;
request.send();
}捕获(e){
//无法使用代理获取
}
}
}
}
/**
*因为IE中的XDomainRequest对象不处理响应XML,
*此函数充当中介,并尝试解析XML和
*返回一个DOM文档。
*
*@param XDomainRequest xdr XDomainRequest对象
*@param string type要返回的数据类型
*
*@返回混合
*/
功能负载(xdr,类型)
{
var-responseText=xdr.responseText,数据类型=type | |“”;
if(dataType.toLowerCase()=“xml”
&&响应文本的类型==“字符串”){
//如果预期的数据类型是xml,则需要将其从
//XML DOM对象的字符串
var-doc;
试一试{
if(window.ActiveXObject){
doc=新的ActiveXObject('Microsoft.XMLDOM');
doc.async='false';
doc.loadXML(responseText);
}否则{
var parser=新的DOMParser();
doc=parser.parseFromString(responseText,'text/xml');
}
退货单;
}捕获(e){
//解析XML进行转换时出错,请返回responseText
}
}
返回响应文本;
} 
函数testGet()
{
//getCORS('http://ucommbieber.unl.edu/CORS/cors.php,null,函数(数据){alert(数据);});
getCORS('http://www.google.com,null,函数(数据){alert(数据);});
}
CORS示例
测试获取
此页面使用CORS从另一台服务器检索内容


CORS是您需要在请求的服务器端添加的配置,而不是客户端。在这里,您必须在Google端添加HTTP头。

只是为了澄清:CORS头是由web资源设置的,用于告诉客户端可以从其他域加载它。作为客户端,您不能设置CORS头。因此,只有当你可以更改你想要访问的站点的标题时,这才有效。这是不正确的。我尝试过其他网站,那里没有与CORS相关的HTTP头。我只在我的web服务器中使用,以允许我的客户端连接其他web站点。
<%
 response.addHeader("Access-Control-Allow-Origin", "http://www.autocom.dk  http://ucommbieber.unl.edu/CORS/cors.php");

 response.addHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
 response.addHeader("Access-Control-Allow-Headers" , "X-Requested-With");
 response.addHeader("Access-Control-Max-Age", "86400");
 %>

<html>
<head>
<script type="text/javascript"       src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">

function getCORS(url, data, callback, type) {
try {
    // Try using jQuery to get data
    jQuery.get(url, data, callback, type);
    // Tr
    jQuery.get(url, data, function(data, textStatus, jqxhr){
      alert("success" + data + " text status:" + textStatus + "  ---" +  jqxhr.getAllResponseHeaders());
    });
} catch(e) {
    // jQuery get() failed, try IE8 CORS, or use the proxy
    if (jQuery.browser.msie && window.XDomainRequest) {
        // Use Microsoft XDR
        var xdr = new XDomainRequest();
        xdr.open("get", url);
        xdr.onload = function() {
            callback(handleXDROnload(this, type), 'success', this);
        };
        xdr.onreadystatechange() = function()
        {
          alert(xdr.getAllResponseHeaders());
        }
        xdr.send();
    } else {
        try {
            // Ancient browser, use our proxy
            var mycallback = function() {
                var textstatus = 'error';
                var data = 'error';
                if ((this.readyState == 4)
                    && (this.status == '200')) {
                    textstatus = 'success';
                    data = this.responseText;
                }
                callback(data, textstatus);
            };
            // proxy_xmlhttp is a separate script you'll have to set up
            request = new proxy_xmlhttp();
            request.open('GET', url, true);
            request.onreadystatechange = mycallback;
            request.send();
        } catch(e) {
            // Could not fetch using the proxy
        }
     }
   }
 }

/**
 * Because the XDomainRequest object in IE does not handle response XML,
 * this function acts as an intermediary and will attempt to parse the XML and
 * return a DOM document.
 *
 * @param XDomainRequest xdr  The XDomainRequest object
 * @param string         type The type of data to return
 *
 * @return mixed
 */

function handleXDROnload(xdr, type)
{
var responseText = xdr.responseText, dataType = type || "";

if (dataType.toLowerCase() == "xml"
    && typeof responseText == "string") {
    // If expected data type is xml, we need to convert it from a
    // string to an XML DOM object
    var doc;
    try {
        if (window.ActiveXObject) {
            doc = new ActiveXObject('Microsoft.XMLDOM');
            doc.async = 'false';
            doc.loadXML(responseText);
        } else {
            var parser = new DOMParser();
            doc = parser.parseFromString(responseText, 'text/xml');
        }
        return doc;
    } catch(e) {
        // ERROR parsing XML for conversion, just return the responseText
    }
  }
  return responseText;
 } 

function testGet()
{
  //getCORS('http://ucommbieber.unl.edu/CORS/cors.php', null, function(data){alert(data);});
  getCORS('http://www.google.com', null, function(data){alert(data);});
}

</script>
</head>
<body>
<h1>CORS Examples</h1>

<p>Test GET
      This page retrieves content from another server, using CORS<br />
   <a href="#" onclick="testGet(); return false;">Get content from another server</a>
</p>

</body>
</html>