【问题标题】:Cannot Read Response Header using XMLHttpRequest.getAllResponseHeaders() for http://www.google.com无法使用 http://www.google.com 的 XMLHttpRequest.getAllResponseHeaders() 读取响应标头
【发布时间】:2023-03-21 19:47:01
【问题描述】:

我在标头中使用了 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>

【问题讨论】:

  • 澄清一下:CORS 标头由 Web 资源设置,以告诉客户端可以从其他域加载它。 作为客户端,您不能设置 CORS 标头。因此,这只有在您可以更改要访问的站点的标题时才有效。

标签: jquery httpresponse cors


【解决方案1】:

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

【讨论】:

  • 那不正确。我已经尝试过不存在与 CORS 相关的 HTTP 标头的其他网站。我只在我的网络服务器中使用过,以允许我的客户端连接其他网站。
猜你喜欢
  • 1970-01-01
  • 2013-06-06
  • 2021-05-26
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 2015-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多