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

问题描述 投票:2回答:1

我在发出跨域请求的指令中有代码。在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时,都会收到响应,因此它肯定存在。任何帮助,将不胜感激。

angularjs google-chrome firefox cross-domain
1个回答
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没有提供非常有用的错误消息。

© www.soinside.com 2019 - 2024. All rights reserved.