javascript - Internet Explorer 10 忽略 XMLHttpRequest 'xhr.withCredentials = true'

标签 javascript ajax internet-explorer cors

我目前在使用 IE10 进行跨域 ajax 调用时遇到问题(在 IE10 模式下,不兼容)。

情况: 我有两个域,http://ahttp://b。我为 http://b 设置了 cookie。我当前位于 http://a 页面。

我想使用 XMLHttpRequest 向 http://b 发出 CORS 请求(根据 http://blogs.msdn.com/b/ie/archive/2012/02/09/cors-for-xhr-in-ie10.aspx 应该可以工作),并在请求中包含 cookie。 JS如下:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://b', true);
xhr.withCredentials = true;
xhr.send();

这应该确保 cookie 附加到请求中;但是,Fiddler 跟踪显示没有附加 cookie,并且我收到 401: Access Denied

服务器配置为使用 CORS,它包含访问控制 header :

Access-Control-Allow-Origin: http://a
Access-Control-Allow-Credentials: true

(这应该没有任何区别,因为没有 OPTIONS 预检请求,并且 IE 发送的第一个请求是 GET,并且 cookie 不存在,因此导致 401)。

此外,JS 代码片段在 Firefox 和 Opera 中都可以正常工作。

最佳答案

这可能是同样的旧 IE P3P 问题。使用 IE 的默认设置,如果设置的 cookie 响应中没有包含 P3P header ,则该 cookie 会被标记为“仅限第一方”。这意味着在第三方上下文中,例如 iframe 或 CORS 请求,IE 将拒绝发送 cookie。

要修复此问题,您需要在设置 cookie 时提供 P3P header 。请参阅http://msdn.microsoft.com/en-us/library/ms537343%28v=vs.85%29.aspx了解详情。

更新:链接现已失效,但您可以在 the Internet Archive 处看到它

关于javascript - Internet Explorer 10 忽略 XMLHttpRequest 'xhr.withCredentials = true',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12643960/

相关文章:

javascript - 处理代码时锁定整个页面

javascript - 将 css 选择器与 RegExp 匹配在浏览器中不起作用

javascript - 在 awesomeplete 中向下拉自动完成添加图像,提交按钮然后向下移动,重置其位置?

javascript - 如何序列化与 jQueryUI 可排序列表的当前项相关的数据?

javascript - 无法从 ajax .done 或 .fail 中更改 CSS

css - IE7 中渲染的字体宽度比其他浏览器大

javascript - :before pseudo-class content in IE10 not working 的宽度

javascript - 有哪些 javascript 树数据结构可用?

javascript - 如何在评论多的时候让评论区自动滚动

javascript - swal().then(function ()) 未在 Internet Explorer 11 中触发