Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax Redirection Handling

I bumped into a situation I've never had to address before. I have a customer who needs to make an ajax request to a URL. For internal reasons, that URL redirects to another URL whose status code needs to be accessed. Is this kind of multi-request scenario handled natively by Ajax requests?

A quick test using jQuery seems to handle the 302, do the redirection and return the content of the targeted page (I'll only need the status code in production, but the content is what "proves" the correct page is being accessed), but I can't find any indication that I can expect this to work universally. I don't know what, if any, library the client will use. Moreover, other clients are likely to use this same URL in the future and it needs to be handled the same way.

Thanks.

like image 972
Rob Wilkerson Avatar asked Nov 12 '10 20:11

Rob Wilkerson


1 Answers

You can expect this to work universally, since it's not jQuery doing the handling. This is specified behavior of the underlying XmlHtpRequest browser object that all libraries use for AJAX calls.

You can find the behavior specified here by the W3C:

If the origin of the URL conveyed by the Location header is same origin with the XMLHttpRequest origin and the redirect does not violate infinite loop precautions, transparently follow the redirect while observing the same-origin request event rules.

I've emphasized the most relevant bit here, as long as it doesn't violate same-origin policy rules (it shouldn't on your same domain), the XmlHttpRequest will get the content of final page it gets redirected to. In other words, it's the behavior you're seeing with jQuery now...it's just not jQuery doing it.

like image 119
Nick Craver Avatar answered Oct 12 '22 00:10

Nick Craver