ie9 ajax跨域withCredentials不发送cookie

时间:2014-05-27 13:46:56

标签: javascript internet-explorer cookies internet-explorer-9 cross-domain

我想用jquery ajax制作一个跨域xhr 我正在使用一个特殊的库,允许ie9支持跨域
http://cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.0/jquery.xdomainrequest.min.js

浏览器发送xhr但没有cookie(凭据)
以下代码在http://first_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 friefox和chrome中完美运行

ie 9 screen debugger shot ie 11 screen debugger shot

解答:

在这个问题上似乎有2个意见:

  1. 这是不可能的
  2. 使用MoonScript lib
  3. 两者都有效,因为"无法在IE9及更早版本中制作跨域ajax请求并发送cookie。"

    BUT! MoonScript诀窍(不知何故),它适用于我。

    所以我建议你试试MoonScript

2 个答案:

答案 0 :(得分:2)

  1. 只能在IE9及更早版本中通过XDomainRequest进行交叉原始请求,而不是通过XMLHttpRequest。您正在使用的库在这些浏览器中委托给XDomainRequest
  2. 在IE9及更早版本中无法进行Credentialed跨源ajax请求,因为XDomainRequest不支持此功能(不支持withCredentials)。

答案 1 :(得分:0)

如果您使用的是jquery $ .ajax,那么有一个无缝的解决方案 只需包含“MoonScript”JQuery ajax-transport就可以了! https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

相关问题