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

时间:2012-10-03 11:43:35

标签: jquery httpresponse cors

我在标头中使用了CORS(跨源资源共享),以允许客户端连接到其他Web资源。我尝试连接到https://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>

1 个答案:

答案 0 :(得分:0)

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