Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for XHR HEAD requests to not follow redirects (301 302)

Tags:

Is it possible to send an xhr HTTP HEAD request to only get header response for the first request and not automatically follow 301, 302 like redirects? I'm only interested in getting the new location of the url. Example:

var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(data) {     if (xhr.readyState == 4) {         if (xhr.status == 301 || xhr.status == 302) {             // Get new location url don't GET it         }     } }; xhr.open('HEAD', url, true); xhr.send(); 

http://www.w3.org/TR/XMLHttpRequest/#infrastructure-for-the-send-method seems to specify that requests should be followed, is there a way to stop this?

like image 347
antonj Avatar asked Sep 29 '10 10:09

antonj


People also ask

Does XHR follow redirect?

Yes, XMLHttpRequest object (according to the standard) must automatically handle redirection without giving the client code a chance to do anything about it.

Which is better 301 or 302 redirect?

HTTP redirect code 301—a 301 redirect for short—is best for SEO if you've moved content permanently. If you've moved content temporarily, a 302 redirect is best.

Is 302 permanent redirect?

What is a 302 redirect? Whereas a 301 redirect is a permanent relocation of your URL, a 302 redirect is a temporary change that redirects both users and search engines to the desired new location for a limited amount of time, until the redirect is removed.

When would it be necessary to 302 redirect every page on a site?

302 redirect should only be used if it is a temporary change, and they often get used because it's easier to create that instance than the permanent 301 redirect. For an overview of all HTTP status codes, read this quick guide on SEO best practices for all HTTP status codes.


2 Answers

There isn't, this isn't exposed behavior you can stop.

It's because of the spec you linked already, the specified behavior is that XmlHttpRequest should transparently follow redirects...under the covers unfortunately, and not in a way you can prevent.

It's this way to try and make things easier, if resources move, etc...but when it was designed and the spec laid out, all these redirection services weren't out there. There just wasn't a strong need for any other behavior or ability to prevent it, I think with as many redirects hitting the web not we'll see the ability added, but who knows when every browser would support it.

like image 175
Nick Craver Avatar answered Oct 03 '22 09:10

Nick Craver


The W3C specification requires that redirects are followed automatically, as @Nick suggested in the other answer. However a property to disable redirects is being considered by the W3C for a future version of this specification:

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

  • load event and onload attribute;
  • error event and onerror attribute;
  • progress event and onprogress attribute;
  • abort event and onabort attribute;
  • Timers have been suggested, perhaps an ontimeout attribute;
  • Property to disable following redirects;
  • responseXML for text/html documents;
  • Cross-site XMLHttpRequest;
  • responseBody to deal with byte streams;
  • overrideMimeType to fix up MIME types;
  • getRequestHeader() and removeRequestHeader().

However, I wouldn't hold my breath until this is implemented by all browsers. You may want to use a server-side proxy to handle this. Simply write a short script to do a HEAD request in your favourite server-side language/framework, and then use Ajax to query this service. You would also be able to do requests to third-party domains through the proxy as a positive side-effect.

like image 21
Daniel Vassallo Avatar answered Oct 03 '22 07:10

Daniel Vassallo