好的,我已经找遍了这个。基本上我们使用的是跨域请求的 $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 无法加载假主机/示例。请求的资源上不存在“Access-Control-Allow-Origin” header 。因此不允许访问 Origin 'mylocalhost:5750'。
然而,在 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"}
我在使用 AngularJS v1.2.16
最佳答案
我想我找到了答案,看起来您必须在 asp.net 管道中注入(inject)正确的 CORS header ,如前所述 here .
关于javascript - AngularJS $http 从失败的 CORS 请求返回状态码 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25506165/