Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - $http error callback not working even though error response returned from the Web API

Tags:

I am calling a method from the server which returns an error response like (400 & 500 errors) but my AngularJS error callback is not getting called and always the success callback is getting called even though my status code contains 400's or 500's. Can someone please tell what am I doing wrong? Please see the angular & WebAPI code below:

AngularJS code:

$http.get("/api/employee")
    .success(function (data, status, headers, config) {
        console.log(data);
        return data;
    }).error(function (data, status, headers, config) {
        alert("error");
        return status;
});

Web API code:

public HttpResponseMessage Get(EmployeeModel employee)
{
     if (!ModelState.IsValid)
     {
         return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
     }
     //some other code
}