$resource - handling HTTP response status on error, HTTP interceptors

12,655 views
Skip to first unread message

alex.k...@gmail.com

unread,
Sep 20, 2012, 5:09:20 AM9/20/12
to ang...@googlegroups.com
Hi guys,
I'm using $resource for downloading the data from the server. Using $resources looks like the most elegant solution for REST API's but I'm facing a problem reading HTTP status when downloading wasn't successful, status is always equal to 0.

Examples are in coffescript

resource  = $resource server.entitiesURL,
        {resource:'', callback:'JSON_CALLBACK'}, 
        {query:method:'JSONP', isArray:true}

resource.query {resource:key},
      ((response) =>
        #OK
      ),
      ((response) =>
        console.log response.status
      )

Error handler always output response.status 0.


The same happens when I'm using HTTP interceptors, it never gets to 404 since the status is always 0

  interceptor = ["$rootScope", "$q", (scope, $q) ->
    success = (response) ->
      return response

    error = (response) ->
      status = response.status

      switch status
        when 404
          #do stuff
          $q.reject response
        else
          $q.reject response

    (promise) ->
      promise.then success, error
  ]
  $httpProvider.responseInterceptors.push interceptor

Any ideas how to read "a real" status from the response? 
Thanks!

Pawel Kozlowski

unread,
Sep 21, 2012, 3:36:27 PM9/21/12
to ang...@googlegroups.com
Alex,

I'm not coffee scripter so hard to see what is going on but the JS
version works OK, I mean, you've got access to the status in the error
callback:
Check this: http://plnkr.co/edit/mV9Xzk?p=preview

Cheers,
Pawel
> --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To post to this group, send email to ang...@googlegroups.com.
> To unsubscribe from this group, send email to
> angular+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/angular?hl=en.
>
>



--
Question? Send a fiddle
(http://jsfiddle.net/pkozlowski_opensource/Q2NpJ/) or a plunk
(http://plnkr.co/)
Need help with jsFiddle? Check this:
http://pkozlowskios.wordpress.com/2012/08/12/using-jsfiddle-with-angularjs/

Looking for UI widget library for AngularJS? Here you go:
http://angular-ui.github.com/

alex.k...@gmail.com

unread,
Sep 24, 2012, 6:40:18 AM9/24/12
to ang...@googlegroups.com, alex.k...@gmail.com
Hi Pawel,
thanks for reply. Just figured out when you can get a 0 status:  getting resource from another domain or getting it from the same domain without using the relative path,

For example: $resource('404.url') works just fine but, using $resource('http://plnkr.co/404.url'); returns 0 status. 
In fact, both cases works in the same way, the page will request the resource and trigger the correct handlers(complete or error) depending on server response but, using $resource('http://plnkr.co/404.url'); doesn't provide the correct response.status (always 0). So, what is the correct way of getting the data from another domain using $resources when I need to handle different response statuses?

Thanks

Pawel Kozlowski

unread,
Sep 26, 2012, 1:58:21 PM9/26/12
to ang...@googlegroups.com
Hi!

On Mon, Sep 24, 2012 at 12:40 PM, <alex.k...@gmail.com> wrote:
> Hi Pawel,
> thanks for reply. Just figured out when you can get a 0 status: getting
> resource from another domain or getting it from the same domain without
> using the relative path,
>
> For example: $resource('404.url') works just fine but, using
> $resource('http://plnkr.co/404.url'); returns 0 status.

Ah, I see! This seems to happen where the OPTIONS request (xhr
pre-fly) is failing.
I don't know what should be the correct behavior in this case thought.
If this is hitting you hard I would suggest doing some reading on
OPTIONS headers, poking around angular sources and eventually opening
an issue report...

Sorry, don't have enough knowledge to help straight away and no time
to dig into it :-(

Cheers,
Pawel

alex.k...@gmail.com

unread,
Sep 27, 2012, 8:56:10 AM9/27/12
to ang...@googlegroups.com
I'ts quite blocking me at this point since the communication is built on $resouces. Using relative path will solve the problem but I cannot use it because UI/backend are runing on different domains.

Here is modified example including both cases:

Cheers.

ande...@marcond.es

unread,
Nov 7, 2012, 9:46:06 AM11/7/12
to ang...@googlegroups.com
I have the same problem. Do you resolved this?

focu...@gmail.com

unread,
Nov 7, 2012, 10:55:27 AM11/7/12
to ang...@googlegroups.com
+1, been having the same problem.

John Saturnus

unread,
Nov 8, 2012, 5:57:58 AM11/8/12
to ang...@googlegroups.com
+1, been having the same problem. very anoying!

Peter Bacon Darwin

unread,
Nov 8, 2012, 6:01:54 AM11/8/12
to ang...@googlegroups.com
It is not too difficult to knock up a promised-based resource service if you have a specific requirement and are not trying to cover every possibility.  Take a look at Pawel's MongoLabResource project for some ideas.
Pete

On 8 November 2012 10:57, John Saturnus <jsat...@gmail.com> wrote:
+1, been having the same problem. very anoying!

Tiago Roldão

unread,
Nov 12, 2012, 12:01:45 PM11/12/12
to ang...@googlegroups.com
Could you help me on this one: i have no idea how the mongolabResourceHttp is even possible! How is it making cross-domain ajax requests?

Peter Bacon Darwin

unread,
Nov 12, 2012, 1:39:30 PM11/12/12
to ang...@googlegroups.com
CORS

Tiago Roldão

unread,
Nov 13, 2012, 9:42:09 AM11/13/12
to ang...@googlegroups.com
Yeah, I figured it out eventually :) but the problem remains (had to get a mongolab account to test it, damn!) When a cross-domain request - or any request that uses an absolute URL - fails, no information is processed - not the return (which can be there, be it an error code and/or message), neither the status code (always comes back as 0, rather than 404, 503, 500, etc...). This is true even in the case of mongolabResourceHttp, so it really seems to be a bug.

Tiago Roldão

unread,
Nov 13, 2012, 9:48:19 AM11/13/12
to ang...@googlegroups.com
BTW: the problem doesn't seem to be with $resource directly, but rather with $http, a bug which $resource inherits

Peter Bacon Darwin

unread,
Nov 13, 2012, 12:49:40 PM11/13/12
to ang...@googlegroups.com

Tyson Nero

unread,
May 25, 2013, 10:00:45 PM5/25/13
to ang...@googlegroups.com
This issue still exists in 1.0.6. I opened a new bug as the other one was specific to JSONP and was closed:

Nick Portokallidis

unread,
Sep 17, 2013, 5:36:58 PM9/17/13
to ang...@googlegroups.com
+1 I am having the same problem here with code 0. i had a little dig up and found on stackoverflow this:
delete $http.defaults.headers.common['X-Requested-With'];
but didn't seem to fix this.

Muhammad Ali

unread,
Mar 30, 2014, 4:14:04 PM3/30/14
to ang...@googlegroups.com
Is this an issue with angularjs or the underlying browsers? i'm getting the same problem -- status = 0.
Reply all
Reply to author
Forward
0 new messages