我正在尝试向具有不同域的服务器发出 POST 请求,这需要基本身份验证。
我已经尝试过 beforeSend 和 withCredentials 的所有组合,但是基本的 auth 标头从未在 OPTIONS 预检请求中发送。
$.ajax({
url: anotherdomain,
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
dataType:'json',
xhrFields: {
withCredentials: true
},
crossDomain: true,
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'Basic *');
xhr.withCredentials = true;
}
我似乎可以使此请求成功的唯一方法是将 dataType 设置为“jsonp”,或将请求“type”设置为 GET。
从另一个域服务器中删除 OPTIONS 请求的基本身份验证要求是解决此问题的唯一方法吗?
谢谢。