【发布时间】:2018-04-10 03:14:50
【问题描述】:
我需要在标头中使用“授权”执行 CORS 请求类型 GET,但由于某种原因它不起作用...我收到 307 内部重定向错误“预检响应无效(重定向)”或者我得到了401 Unauthorized 如果使用 HTTPS。
我试过这个:
$.ajax({
url: "http://exemple.com/api/ajax.php",
type: "GET",
withCredentials: true,
crossDomain: true,
dataType: 'jsonp',
beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer A5MjE2DA1LCJzaWduIjoiZGJYTExNjM==');},
success: function() {
alert('Success!' + xhr.responseText);}
});
还有这个:
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://exemple.com/api/ajax.php", true);
xhr.setRequestHeader("Authorization", "Bearer A5MjE2DA1LCJzaWduIjoiZGJYTExNjM==");
xhr.onload = function () {
console.log(xhr.responseText);
};
xhr.send();
得到错误 307 内部重定向错误“预检响应无效(重定向)”
然后在服务器端,("Access-Control-Allow-Origin: *"); 看起来没问题,那么有什么问题吗?如何做到这一点?
【问题讨论】:
标签: javascript ajax xmlhttprequest cors authorization