开发者

Ajax Redirection Handling

开发者 https://www.devze.com 2023-01-25 01:25 出处:网络
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 nee

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 conte开发者_Go百科nt 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.


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.


If you look at firebug, you can actually see the redirect happening. I think you are right that you can't depend on this for every browser. But my inclination would be that most recent browsers will handle the http connection for you and do the redirect.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号