Following redirect_url in XMLHttpRequest response

Chris's icon

I’m working on some JavaScript that retrieves a resource from a URL, and would like to make it as robust as possible, handling all the various potential errors and responses that dodgy connections and unpredictable servers might throw back at the patch.

Max’s implementation of XMLHttpRequestdeviates from the W3C specification in that it doesn’t follow redirects when a server returns a status code of 301, 302, 303, 307, or 308. Of course, not being a real browser, it’s Max’s prerogative to ignore such a spec, especially if I have understood correctly and XMLHttpRequest is actually a wrapper for libcurl (or does that only apply to the maxurl object?)

Nevertheless, this means if you want to emulate the standard behaviour, you need to manually handle responses with redirects. This is less of a problem with status codes 0, 4xx or 5xx, where one will almost always want to just throw an error, but with 3xx statuses, the most likely desire is to follow the redirect to reach the endpoint you actually wanted. In my view, Max should do this by default, or at least optionally (curl -L turns on redirect following on the command line for example).

In the meantime, does anyone have any ideas/tips on how to achieve this in JavaScript? The maxurl object returns a redirect_url property in its 3xx responses, but I can’t seem to find any equivalent property passed to an XMLHttpRequest’s callback.