javascript AngularJS $http 从失败的 CORS 请求返回状态代码 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25506165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS $http returns status code 0 from failed CORS request
提问by hammer
Okay, I've looked all over for this. Basically we're using $http request that ARE cross domain requests. Our server allows the domain and when a request returns 200, everything is OK. However, anytime our server returns an error, 500, 401, whatever, Angular thinks it's a CORS issue.
好的,我已经找遍了这个。基本上我们使用的是跨域请求的 $http 请求。我们的服务器允许域,当请求返回 200 时,一切正常。但是,无论何时我们的服务器返回错误 500、401 等,Angular 都会认为这是 CORS 问题。
I debugged the response with Fiddler to verify my server IS returning a 500, yet Angular chokes on it.
我用 Fiddler 调试了响应,以验证我的服务器是否返回 500,但 Angular 却卡住了它。
Here's the request:
这是请求:
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
});
Then in the console, I will see this:
然后在控制台中,我会看到:
XMLHttpRequest cannot load fakehost/example. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'mylocalhost:5750' is therefore not allowed access.
XMLHttpRequest 无法加载 fakehost/example。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问 Origin 'mylocalhost:5750'。
Yet, in fiddler, the true response is:
然而,在 fiddler 中,真正的反应是:
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"}
I'm on AngularJS v1.2.16
我在 AngularJS v1.2.16