【发布时间】: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