Search code examples
javascriptajaxxmlhttprequest

Prevent redirection of XMLHttpRequest


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


Solution

  • 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 were considering it for a future release:

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

    • Property to disable following redirects;

    but the latest specification no longer mentions this.