跨域请求在Firefox中返回状态代码0,但在Chrome中返回200

时间:2014-07-10 15:28:33

标签: angularjs google-chrome firefox cross-domain cors

我在发出跨域请求的指令中有代码。当它在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时,我得到一个响应,所以它肯定存在。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

原来问题是我需要像这样指定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没有提供非常有用的错误消息。