【问题标题】:Cross Domain request returns status code 0 in Firefox but 200 in Chrome跨域请求在 Firefox 中返回状态码 0,但在 Chrome 中返回 200
【发布时间】:2014-09-01 01:32:06
【问题描述】:

我在发出跨域请求的指令中有代码。当它在 Chrome 中运行时,它工作得很好。当它在 Firefox 或 IE 11 中运行时,我得到一个没有数据的状态代码 0。我的代码是:

$http({method: 'GET', url: 'https://mycrossdomain.url'})
    .success(function(data, status, headers, config) {
        alert('success');
    })
    .error(function(data, status, headers, config) {
        alert('error');
    })

在 Firefox 和 IE 中我收到“错误”警报,在 Chrome 中我收到“成功”并且我可以查看数据。

为什么会发生这种情况?服务器显然支持跨域请求,因为它可以在 Chrome 中运行。当我在所有浏览器中导航到 URL 时,我都会收到响应,因此它肯定存在。任何帮助将不胜感激。

【问题讨论】:

  • 尝试比较 Chrome 和 Firefox 中的请求和响应标头,以验证它们是否相同。另外,每个浏览器会提示什么,“成功”还是“错误”?
  • 除了响应标头中的标头相同之外,Chrome 具有“Access-Control-Allow-Credentials”= true 和“Access-Control-Allow-Origin”= 我的服务器域。这不在 Firefox 的响应中。
  • 另外,我可以在 Firefox 的控制台中看到我收到一条消息“跨域请求被阻止...”等。

标签: angularjs google-chrome firefox cross-domain


【解决方案1】:

原来问题是我需要像这样指定withCredentials

$http.get('https://mycrossdomain.url', { withCredentials: true})
    .success(function(data, status, headers, config) {
        alert('success');
    })
    .error(function(data, status, headers, config) {
        alert('error');
    })

这是因为我需要将用户凭据(证书)传递给其他域。不幸的是,Angular 没有提供非常有用的错误消息。

【讨论】:

  • 但为什么它可以在 Chrome 中运行?似乎它应该在 Chrome 中失败。
  • 我不确定,也许 Chrome 会自动为 CORS 请求传递凭据。
  • 如果您能够在公共场所重现该错误,那就太好了。这听起来确实像一个错误,Chrome 不应该在未经许可的情况下发送凭据。
  • 不幸的是,我在专用网络上,无法共享客户的代码。很奇怪,目标服务器在 Firefox 和 IE 中返回 500(这是有道理的,因为服务器需要凭据),而在 Chrome 中,我得到了 200 和结果。 ¯\_(ツ)_/¯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
  • 2014-07-18
  • 2013-01-11
  • 1970-01-01
  • 2015-03-14
  • 2011-04-22
相关资源
最近更新 更多