2012-10-03 37 views
0

响应头我已经在头中使用CORS(跨源资源共享),以允许客户端连接到其他网络资源。我试图连接到https://www.google.com,但无法获取标题的任何信息。我需要从标题中读取日期。无法读取使用XMLHttpRequest.getAllResponseHeaders()为http://www.google.com

这里是我使用从http://saltybeagle.com/2009/09/cross-origin-resource-sharing-demo/

<% 
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> 
+0

只是为了阐明:一个CORS头由Web资源设置,告诉客户可以从其他域加载CORS头。 **作为客户端,您不能设置CORS标头**。因此,只有在您可以更改要访问的网站标题时才有效。 – Odi

回答

0

CORS代码的配置,你需要在要求,而不是客户端的服务器侧添加。在这里,你必须在Google上添加HTTP标头。

+0

那是不正确的。我曾尝试过其他不存在与CORS相关的HTTP标头的网站。我只在我的Web服务器上使用,以允许我的客户端连接其他网站。 –