html - CORS withCredentials XHR 预检不在 Firefox 中发布 Cookie

标签 html firefox cookies cross-domain

我正在尝试使用凭据进行 CORS XHR 发布。它在 Chrome 中运行良好,但在 Firefox 中运行不佳。 cookie 不存在于飞行前请求 header 中,因此我看到了 302。这在 Chrome 中完美运行,因为 cookie 位于飞行前请求 header 中,随后的 POST 通过。

为什么这在 FF 中不起作用?我错过了什么?

// assume url, boundEventHandler and uploadData are defined, as this definitely works in Chrome
var xhr = new XMLHttpRequest(); 
xhr.open("POST", url, true); 
xhr.addEventListener ("readystatechange", boundEventHandler, false); 
xhr.withCredentials = true;  // FWIW, I've also tried the string 'true'
xhr.send(uploadData);

有什么想法吗?我看到一些帖子说我可以在服务器端代理请求,但我更愿意按照 CORS 规范让它工作。

谢谢!

最佳答案

根据 https://www.w3.org/TR/cors/#resource-preflight-requests 的规范预检请求从不包含 cookie。具体来说,规范说:

  • Exclude user credentials.

并且链接到 https://www.w3.org/TR/cors/#user-credentials其中说:

The term user credentials for the purposes of this specification means cookies, HTTP authentication, and client-side SSL (...).

就是说,您上面引用的代码片段根本不应该涉及预检:没有上传事件监听器,方法很简单,也没有设置作者标题。因此,如果您真的看到预检请求,第一个问题就是为什么会这样。您在 Firefox 中是否有任何扩展可能会修改您的 XMLHttpRequest 对象?

关于html - CORS withCredentials XHR 预检不在 Firefox 中发布 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10133497/

相关文章:

firefox - 如何让 Firefox 在同一窗口中打开通过 WebDriver 打开的所有链接?

JavaScript Cookie 不基于 URL 属性写入

javascript - 在 Safari 上设置跨站 cookie

html - 在 Canvas 中剪出圆形图像

Firefox Quantum SharedWorker 无法工作

html - 如何使用CSS制作透明边框?

javascript - 在 Firefox 中设置 top.location.hash 为 %20

node.js - Node Express - req.cookies 和 req.session.cookie 的区别

javascript - Internet Explorer CSS 转换问题

javascript - 使用 document.getElementById 将文本传递给 JavaScript