問題描述
好的,我已經找遍了這個.基本上我們使用的是跨域請求的 $http 請求.我們的服務器允許域,當請求返回 200 時,一切正常.但是,無論何時我們的服務器返回一個錯誤,500、401 等等,Angular 都會認為這是一個 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,但 Angular 會阻塞它.
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:
XMLHttpRequest 無法加載 fakehost/example.請求的資源上不存在Access-Control-Allow-Origin"標頭.因此,Origin '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 請求返回狀態碼 0的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!