response status code when using JSONP
Resource should return correct HTTP status no mater what path is used relative/absolute. Example: http://plnkr.co/edit/aKyhX4?p=preview
Original discussion: https://groups.google.com/forum/?fromgroups=#!topic/angular/ycPbZSVHkuA
Thanks
Hi oilart,
Using an absolute url in your request causes a cross-domain request, which we know fails by default due to the same origin policy: http://en.wikipedia.org/wiki/Same_origin_policy -- In other words, we can't make a request from one domain. This includes requests to different subdomains, which is the case here. If you open web inspector in chrome and use the console view you'll notice the error being thrown: 'XMLHttpRequest cannot load http://plnkr.co/404.url. Origin http://run.plnkr.co is not allowed by Access-Control-Allow-Origin.'
Hi Kevin, thanks for replay. Agree, this wasn't a good example. I'm using $resource for downloading the data from the server by using JSONP, so I don't have cross-domain issue but, still cannot read the correct status code. I will update the example. Thanks.
Updated: http://plnkr.co/edit/aKyhX4?p=preview
Thanks
+1 - Confirmed that the status returns as 0 even when the server allows cross-domain requests when using JSONP
I think this is actually a problem with $http service, http://plnkr.co/edit/2qYJhV?p=preview, which $resource wraps.
Yes, I have the exact same problem using $http. +1, it breaks usability of an app if it requires cross domain requests - no error handling is possible!
same thing here ... I was trying to fetch the status code for a response returning a 401. unfortunately the status returns 0 in that case.
Unfortunately, with the way JSONP works, it isn't really possible to get a status code back unless the server is providing the status code in its object response (not headers).
What Angular does is create a script element with the URL given and the browser will load it as a normal JS file, but it's not a request that is captured by JS or Angular. You can only know success or failure through onload and onerror.
Angular actually creates a different callback that it manages internally. It simply takes the outputted response data and sets it on that intermediate callback object. Then if the response exists, it will call your callback and return a 200 (even if the real response was a 500). If it doesn't have a response, it will return 0 (-2 internally, but these are normalized before returning to you).
I just ran in to this. @jtymes isn't this due to the way JSONP works in AngularJS? Is there no other javascript library that has figured out how to report the status code for JSONP?
@gregwebs Nope, it's not specific to AngularJS and there's no way around it. JSONP itself is pretty hacky, and it's not a specific library's fault - there's just no way to access the header response of a <script> element.
From another library and a SO topic
darn. Rather than pretending to have a status code would it be better to set the status code as null?
Agree with @jtymes - from what I know it is not really possible to get HTTP status codes with the JSONP