0

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

1 回答 1

0

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

于 2012-10-03T12:18:03.490 回答