簡體   English   中英

AngularJS $ http服務永遠不會調用錯誤處理程序

[英]AngularJS $http service never calls the error handler

我有以下代碼:

$http
.post('/api/login', $scope.user)
.success(function (data, status, headers, config) {
  // code
})
.error(function (data, status, headers, config) {
  // code
});

它可以工作,但是即使服務器返回異常(狀態500),錯誤請求(400)或未授權請求(401),它仍會調用成功處理程序。

這是來自服務器的示例響應(我的服務器是ASP.NET Web API 2):

HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcRG9jdW1lbnRzXFRpbWVUYWJsZVxqYW4tMjAxNFwxMVxUaW1lVGFibGVcVGltZVRhYmxlXGFwaVxsb2dpbg==?=
X-Powered-By: ASP.NET
Date: Sun, 23 Feb 2014 10:56:09 GMT
Content-Length: 0

您需要確保在攔截器中為“ responseError”屬性返回了被拒絕的承諾。 如果您只是返回響應或未實現“ responseError”,那么無論狀態碼如何,成功函數都將始終運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM