【发布时间】:2014-10-19 19:10:49
【问题描述】:
好的,我已经找遍了这个。基本上我们使用的是跨域请求的 $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”标头。因此,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
【问题讨论】:
-
确认一下,error.data 属性未定义?
-
不是未定义,而是responseError拦截器中的整个响应是:Object {data: "", status: 0, headers: function, config: Object, statusText: ""}
-
另外,如果我故意发送失败的授权,状态码和数据会正确返回,所以它必须是 500 错误和 CORS 请求的组合:{data: Object, status: 401,标头:函数,配置:对象,状态文本:“未授权”}
标签: javascript angularjs xmlhttprequest cors