简体   繁体   English

AngularJS $ http服务永远不会调用错误处理程序

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

I have this code: 我有以下代码:

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

It works, but even though the server returns an exception (status 500) or a bad request (400) or an unauthorized request (401) it still calls the success handler. 它可以工作,但是即使服务器返回异常(状态500),错误请求(400)或未授权请求(401),它仍会调用成功处理程序。

This is an example response from the server (my server is ASP.NET Web API 2): 这是来自服务器的示例响应(我的服务器是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

You need to make sure you are returning a rejected promise in your interceptor for the 'responseError' property. 您需要确保在拦截器中为“ responseError”属性返回了被拒绝的承诺。 if you are simply returning the response or not implementing the 'responseError' then the success function will always run regardless of the status code. 如果您只是返回响应或未实现“ responseError”,那么无论状态码如何,成功函数都将始终运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM