Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS $http error function never called

Tags:

I have that simple code :

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

It works fine, but the error function is never called, even when I return a 404 (Not Found) from the server... In that case it calls that 'success' function with status = 404...

Is that correct?

Thanks

Fiddler:

Request  GET http://localhost:41234/api/test HTTP/1.1 Host: localhost:41234 Connection: keep-alive Accept: application/json, text/plain, */* X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko)    Chrome/25.0.1364.172 Safari/537.22 Referer: http://localhost:41234/ Accept-Encoding: gzip,deflate,sdch Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: ASP.NET_SessionId=bd1b3rib5j4beub0xbuhb1hm; FormsAuthentication=xxxxx  Response  HTTP/1.1 404 Not Found 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?RDpcUGVzc29hxvY2FyLkFwaVxhcGcg==?= X-Powered-By: ASP.NET Content-Length: 0 
like image 521
Paul Avatar asked Apr 08 '13 20:04

Paul


People also ask

How to set http request header in AngularJS?

To add headers for an HTTP method other than POST or PUT, simply add a new object with the lowercased HTTP method name as the key, e.g. $httpProvider. defaults. headers. get = { 'My-Header' : 'value' } .

What is q defer() in AngularJS?

Simply put you can use $q. defer() to create a Promise. A Promise is a function that returns a single value or error in the future. So whenever you have some asynchronous process that should return a value or an error, you can use $q. defer() to create a new Promise.

Why we use$ q in AngularJS?

$q is integrated with the $rootScope. Scope Scope model observation mechanism in AngularJS, which means faster propagation of resolution or rejection into your models and avoiding unnecessary browser repaints, which would result in flickering UI. Q has many more features than $q, but that comes at a cost of bytes.

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.


1 Answers

I had the same problem and, honestly, follow the hints of this post put me in the wrong direction... so, I share my case/solution so other in my same situation could save time.

I'm using Angular.js 1.2.14 + WebApi 2. this my response for a NotFound status:

Cache-Control:no-cache Content-Length:0 Date:Sat, 15 Mar 2014 14:28:35 GMT Expires:-1 Pragma:no-cache Server:Microsoft-IIS/8.0 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET X-SourceFiles:=?UTF-8?B?QzpcU3ZpbHVwcG9EaXNjaXR1clxhcGlcTWFnMTRcYXBpXGxlc3Nvblw4NA==?= 

As you can see, Content-Lenght:0, but that's ok.

My problem was the uncorrect use of Angular.js interceptor, in particular something like this:

responseError: function (result) {                 // check something                  return result;             } 

returning result without throw an exception or rejecting the promises (as written in docs) makes Angular believe that I want to convert rejection in correct resolution and, after that, success callback is called.

I correct my code as follow:

responseError: function (result) {                                     // check something                  return $q.reject(result);             } 
like image 180
wilver Avatar answered Sep 22 '22 07:09

wilver