小编典典

AngularJS $ http从失败的CORS请求返回状态代码0

angularjs

好的,我已经到处寻找了。基本上,我们使用的是跨域请求的$
http请求。我们的服务器允许该域,当请求返回200时,一切正常。但是,无论何时我们的服务器返回错误500、401,无论如何,Angular都认为这是CORS问题。

我用Fiddler调试了响应,以验证服务器是否返回了500,但Angular阻塞了。

这是请求:

       var params = {
            url: "fakehost/example",
            method: 'GET',
            headers: {
                "Authorization": "Basic encodedAuthExample"
            }
        };

       $http(params).then(
            function (response) { // success

            },
            function (error) { // error 
                 // error.status is always 0, never includes data error msg
            });

然后在控制台中,我将看到:

XMLHttpRequest无法加载fakehost / example。所请求的资源上没有“ Access-Control-Allow-
Origin”标头。因此,不允许访问源“ mylocalhost:5750”。

然而,在提琴手中,真正的回应是:

HTTP/1.1 500 Internal Server Error
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 26 Aug 2014 12:18:17 GMT
Content-Length: 5683

{"errorId":null,"errorMessage":"Index was outside the bounds of the array.","errorDescription":"Stack trace here"}

我正在使用AngularJS v1.2.16


阅读 323

收藏
2020-07-04

共1个答案

小编典典

我想我找到了答案,样子你会在你的asp.net管道注入正确的CORS头,提到这里

2020-07-04