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

ham*_*mer 10 javascript xmlhttprequest cors 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
            });
Run Code Online (Sandbox Code Playgroud)

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

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"}
Run Code Online (Sandbox Code Playgroud)

我在AngularJS v1.2.16上

小智 7

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