angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

response status code when using JSONP

Open oilart opened this issue 14 years ago • 12 comments

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

oilart avatar Sep 27 '12 12:09 oilart

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.'

kevinwestern avatar Oct 01 '12 16:10 kevinwestern

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.

oilart avatar Oct 02 '12 07:10 oilart

Updated: http://plnkr.co/edit/aKyhX4?p=preview

Thanks

oilart avatar Oct 02 '12 08:10 oilart

+1 - Confirmed that the status returns as 0 even when the server allows cross-domain requests when using JSONP

mtburge avatar Nov 11 '12 19:11 mtburge

I think this is actually a problem with $http service, http://plnkr.co/edit/2qYJhV?p=preview, which $resource wraps.

petebacondarwin avatar Nov 11 '12 20:11 petebacondarwin

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!

tiagoroldao avatar Nov 13 '12 14:11 tiagoroldao

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.

mawo87 avatar Nov 27 '12 16:11 mawo87

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).

See the JSONP handling in httpBackend

jtymes avatar Nov 27 '12 17:11 jtymes

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 avatar Dec 12 '12 14:12 gregwebs

@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

jtymes avatar Dec 12 '12 16:12 jtymes

darn. Rather than pretending to have a status code would it be better to set the status code as null?

gregwebs avatar Dec 12 '12 17:12 gregwebs

Agree with @jtymes - from what I know it is not really possible to get HTTP status codes with the JSONP

Owner


Other Repo Issues