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.

This is an example response from the server (my server is 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

Answers


You need to make sure you are returning a rejected promise in your interceptor for the 'responseError' property. if you are simply returning the response or not implementing the 'responseError' then the success function will always run regardless of the status code.


Need Your Help

Cast to a control using the control name as a string

iphone objective-c xcode

I have several textfields (and labels) in my XIB and at some point in my application I build dynamicaly a string which contains a control name (i.e. one of the textfields).

Difference between NSString *const and const NSString *?

objective-c nsstring const

Can someone tell me the difference between the following 2 declarations of NSString?