views:

2961

answers:

3

Is it possible to prevent the browser from following redirects when sending XMLHttpRequest-s (i.e. to get the redirect status code back and handle it myself)?

A: 

Yes, you check to see if the status code (use the status property) is 301 or 302.

W3 schools writeup on XMLHttpRequest

Darren Kopp
By default XMLHttpRequest will follow 301 for you and return the 200 of the subsequent request, annoyingly you can't even determine that a 301 happened
AnthonyWJones
+1  A: 

No you there isn't any place in the API exposed by XMLHttpRequest that allows you to override its default behaviour of following a 301 or 302 automatically.

If the client is running IE on windows then you can use WinHTTP instead to set an option to prevent that behaviour but thats a very limiting solution.

AnthonyWJones
+11  A: 

Not according to the W3C standard for the XMLHttpRequest object (emphasis added):

If the response is an HTTP redirect:

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.

They are considering it for the future though:

This specification does not include the following features which are being considered for a future version of this specification:

  • Property to disable following redirects;
RelaXNow