使用XMLHttpRequest时,CORs不会启用?

时间:2015-03-05 02:18:43

标签: javascript ajax cors

我花了好几个小时尝试从其他域访问资源。

其他SO帖子中引用的

http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/表示只需在支持CORS的浏览器中使用XMLHttpRequest,就应该启用CORS策略。但是我还在 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.nczonline.net/. This can be fixed by moving the resource to the same domain or enabling CORS.

在Firefox 34中使用它时,根据http://caniuse.com/#feat=cors应该足够了。

我正在尝试http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/

中的一个简单示例

                   

<script type="text/javascript">
    function log(msg){
        var output = $('#output');
        output.text(output.text() + " | " + msg);
        console.log(msg);
    }

    function createCORSRequest(method, url){
        var xhr = new XMLHttpRequest();
        if ("withCredentials" in xhr){
            xhr.open(method, url, true);

            log("'withCredentials' exist in xhr");
        } else if (typeof XDomainRequest != "undefined"){
            xhr = new XDomainRequest();
            xhr.open(method, url);

            log("XDomainRequest is being used");
        } else {
            xhr = null;

            log("xhr is null");
        }
        return xhr;
    }

    function main(){
        log("Attempting to make CORS request");

        var request = createCORSRequest("get", "https://www.nczonline.net/");
        if (request){
            request.onload = function(){
                log("LOADED!");
            };

            request.send();
        }
    }

    $(window).load(function(){
        main();
    });
</script>

我得到以下输出:

Attempting to make CORS request 'withCredentials' exist in xhr Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.nczonline.net/. This can be fixed by moving the resource to the same domain or enabling CORS.

在小提琴https://jsfiddle.net/zf8ydb9v/上尝试它会得到相同的结果。是否有其他杠杆需要打开才能使用XMLHttpRequest来使用CORS bBesides?

2 个答案:

答案 0 :(得分:2)

same origin policy(阻止发出CORS请求)用于您的安全性,而不是服务器的安全性:它可以防止恶意脚本使用您的服务器访问其他服务器上的数据饼干。 因此,如果您需要,仍然可以在您的浏览器上自行将其停用。

在Chrome / Chromium中,如果要禁用相同的源策略,可以使用--disable-web-security选项启动它:

chromium-browser --disable-web-security

无论如何,如果你想让它为你的用户工作,如果他们没有在他们的浏览器中禁用这个安全检查,他们将无法发出CORS请求(如果没有进行测试,则不鼓励这样做。)

正如其他答案所述,如果某些服务器认为这样做有用且对用户无害,则可以故意允许这类请求,并且可以使用Access-control标头执行此操作。

此外,如果您仍希望找到向用户提供此类功能的方法,则可以制作Chrome扩展程序which is not bound to the same origin policy

通常的解决方案是在交叉源请求服务器端,将结果返回给您的应用程序。您应该小心编码:将URL提取到服务器很容易引起服务器端软件的安全问题。但是如果你每次都要获取相同的url,你可以在服务器端硬编码,在PHP中看起来像这样:

<?php
    echo file_get_contents("http://your_cross_request/");
?>

然后对此页面(来自同一个来源)发出ajax请求将返回远程URL的内容。

答案 1 :(得分:1)

CORS headers位于服务器发送到请求响应中。如果请求的页面没有发送标题,那么您在库存浏览器中对请求所做的操作无关紧要,您将收到安全性错误

相关的 CORS 标题如下所示,最后一个是最重要的

Access-Control-Allow-Credentials: false
Access-Control-Allow-Methods: GET
Access-Control-Allow-Origin: *

我尝试打开“nczonline.net”,当我查看响应标头时,我没有看到任何这些,因此服务器未配置为允许以这种方式加载

如果您是该网站的管理员,您可能需要考虑在回复中添加所需的标题,可能是特定于允许的来源而不是使用通配符

如果您只是想尝试演示代码并希望与第三方一起尝试,请加载一个发送这些标题的页面,例如developer.mozilla.org

相关问题