AngularJS $ HTTP从失败的CORS请求返回状态code 0 [英] AngularJS $http returns status code 0 from failed CORS request
问题描述
好吧,我已经看了遍这一点。基本上,我们使用是跨域请求$ HTTP请求。我们的服务器允许的域和时请求返回200,一切都OK。但是,我们任何时候服务器返回一个错误,500,401,不管,角度认为它是一个CORS问题。
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.
我调试使用Fiddler的反应来验证我的服务器上它返回一个500,但角扼流圈。
I debugged the response with Fiddler to verify my server IS returning a 500, yet Angular chokes on it.
下面的要求:
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:
XMLHtt prequest无法加载fakehost /例子。无访问控制允许来源标头的请求的资源present。原产地'mylocalhost:5750',因此是不允许访问。
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.
然而,在小提琴手,真正的回应是:
Yet, in fiddler, the true response is:
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
I'm on AngularJS v1.2.16
推荐答案
我想我找到了答案,样子你会在你的asp.net管道注入正确的CORS头,如提到的这里
I think I found an answer, looks like you will have to inject in your asp.net pipeline the correct CORS headers, as mentioned here.
这篇关于AngularJS $ HTTP从失败的CORS请求返回状态code 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!