【问题标题】:Setting CORS call to GET not OPTIONS将 CORS 调用设置为 GET 而不是 OPTIONS
【发布时间】:2015-02-04 15:23:59
【问题描述】:

我正在按如下方式创建 CORS 调用:

createCORSRequest: function(method, url) {
    var xhr = new XMLHttpRequest();
    xhr.withCredentials = true;

    if ("withCredentials" in xhr) {
        // Check if the XMLHttpRequest object has a "withCredentials" property.
        // "withCredentials" only exists on XMLHTTPRequest2 objects.
        console.log("Sending request with credneitials");
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
        // Otherwise, check if XDomainRequest.
        // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }

    xhr.setRequestHeader('Authorization', 'Bearer bf6dcfd4e975a007dc8184be6bcf580c'); //Authorization details needed

    return xhr;
}

问题是这总是作为一个 OPTIONS 调用发送的,服务器根本不处理它。如果我删除

xhr.setRequestHeader('Authorization', 'Bearer bf6dcfd4e975a007dc8184be6bcf580c');

然后它变成一个 GET 请求,但没有访问令牌服务器将不会处理它。

有没有办法在 GET 请求中发送 Authorization Header ?

或者我是否必须修改服务器来处理 OPTIONS 请求? IE。预检等等。

感谢您的帮助。

【问题讨论】:

    标签: javascript xmlhttprequest authorization cors


    【解决方案1】:

    如果您设置了 Authorization 标头,那么您将发出一个复杂的请求,并且您必须在浏览器发出 GET 请求之前处理 OPTIONS 预检。

    如果不处理 OPTIONS 请求,则无法设置标头。

    【讨论】:

    • 我希望情况并非如此。感谢昆汀证实了我的恐惧。
    猜你喜欢
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-21
    • 2017-11-27
    相关资源
    最近更新 更多