繁体   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