在 Firefox 的 jQuery $ajax 调用中使用 xhrFields: withCredentials: true 时的状态码 = 0

Posted

技术标签:

【中文标题】在 Firefox 的 jQuery $ajax 调用中使用 xhrFields: withCredentials: true 时的状态码 = 0【英文标题】:Status code = 0 when using xhrFields: withCredentials: true in jQuery $ajax call with Firefox在 Firefox 的 jQuery $ajax 调用中使用 xhrFields: withCredentials: true 时的状态码 = 0 【发布时间】:2012-07-01 10:22:34 【问题描述】:

我正在使用

xhrFields :  
  withCredentials: true 
 

在 jQuery $ajax 调用中,以便在我的查询中发送会话 cookie。

该调用在我的 apache 日志中提供了正确的状态代码(401/200,取决于是否设置了 cookie),但 Firefox 总是收到状态=0(即 $.ajax() 中的错误) 如果我删除了这个 xhrFields 部分,状态码就可以了(但不发送 cookie)

这是我在 Firefox 中使用 xhrFields 设置收到的响应对象:

"readyState":0,"responseText":"","status":0,"statusText":"error"

我的 Apache 配置启用了 CORS,并且还允许 Access-Control-Allow-Credentials(这里是相应的 HTTP 标头)

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *

AJAX 调用或网络服务器配置中是否缺少某些内容?

注意:这在 Chrome 中运行良好

【问题讨论】:

您是否检查过其他浏览器(如 G Chrome)? 我在 Chrome 上遇到了同样的问题,而且我也在使用完整的来源而不是通配符来源。 【参考方案1】:

您可能必须比 * 更明确地指定 Access-Control-Allow-Origin 标头。

https://developer.mozilla.org/En/HTTP_access_control#Requests_with_credentials 说:

重要提示:在响应凭据请求时,服务器 必须指定域,并且不能使用通配符。

【讨论】:

以上是关于在 Firefox 的 jQuery $ajax 调用中使用 xhrFields: withCredentials: true 时的状态码 = 0的主要内容,如果未能解决你的问题,请参考以下文章

在 Firefox 的 jQuery $ajax 调用中使用 xhrFields: withCredentials: true 时的状态码 = 0

jQuery $.ajax(), $.post 在 Firefox 中将“OPTIONS”作为 REQUEST_METHOD 发送

jquery $.ajax 在 Chrome 或 Firefox 中调用会导致 401 未经授权的响应,但在 IE 中有效

Firefox 和 nodejs 的 jQuery Ajax POST 错误

请在 401 时帮助测试 Firefox jQuery ajax 中的 CORS 问题

仅在 Firefox 中的 JQuery AJAX 异常:“无法在层次结构中的指定点插入节点”(HierarchyRequestError)