我想用jquery做一个跨域的xhr。
我正在使用一个特殊的库,它允许ie9支持跨域
http://cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.0/jquery.xdomainrequest.min.js
浏览器发送xhr,但没有cookie(凭据)。
以下代码在domain.local上运行
$.ajaxSetup({
type: "POST",
cache: false,
crossDomain: true,
data: {},
dataType: 'json',
xhrFields: {
withCredentials: true
}
});
jQuery.support.cors = true;
$.ajax({
cache: false,
async: true,
crossDomain: true,
url: "http://second_domain.local",
beforeSend: function(xhr) {
xhr.withCredentials=true;
},
type: "POST",
data: {},
dataType: "JSON",
success: function(res, textStatus, xhr) {
},
error: function (xhr, ajaxOptions, thrownError) {
}
});php服务器上的设置:
$http_origin = $_SERVER['HTTP_ORIGIN'];
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Authorization');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Origin: '.$http_origin.'');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");它在IE 10,11飞狐和铬中运行得很好。


答案:
在这一问题上似乎有两种意见:
这两种方法都是有效的,因为“无法在IE9和旧版本中提出跨域ajax请求并发送cookie。”
但!MoonScript在某种程度上做到了这一点,而且它对我很管用。
所以我建议你试试MoonScript
发布于 2014-05-27 18:07:57
XDomainRequest,而不是通过XMLHttpRequest。在这些浏览器中使用XDomainRequest委托的库。XDomainRequest不支持这种请求(不支持withCredentials)。发布于 2015-07-07 16:03:52
如果您使用jquery $.ajax,只需包含"MoonScript“JQuery ajax-transport,您就可以设置!https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest,这是一个看似没有意义的解决方案。
https://stackoverflow.com/questions/23891207
复制相似问题