8

I'm aware that redirects are followed automatically, and that I have little/no control over that process. This is fine, but I'm still very interested in where my request ultimately ends up. Is it possible to see what url my request finally ends up at?

I do not want to rely on the returned HTML itself to tell me where I am.

Sample Code:

var originalURL = '/this/will/be/redirected';
$.ajax({
    url: originalURL,
    dataType: "html",
    success: function(data, statusText, jqXHR) {
        var endPointURL = insertMagicHere();
        alert("Our query to " + original + " ended up at " + endPointURL + "!");
    }
});

I'm looking around in jqXHR for it, but no luck so far. (Though, I'm new to all this, probably right under my nose)

JAAulde
  • 19,250
  • 5
  • 52
  • 63
Chuck
  • 1,062
  • 12
  • 28
  • Does [this existing StackOverflow question](http://stackoverflow.com/questions/4465547/capture-redirect-location-of-javascript-xmlhttprequest) help you solve it? Sadly, it looks like there is no way without some sort of server-side modifications to capture the redirected location. – OverZealous Aug 05 '11 at 01:31
  • Agreed. Per the XHR spec, a redirect is silently handled. In my testing, a 302 doesn't even trigger `readystatechange`! – JAAulde Aug 05 '11 at 02:52
  • Short answer: not in jQuery alone. Last time I tried, even registering all the event listeners available didn't allow me to detect the redirect. You probably want a non-cross-browser solution (i.e. a small flash movie and possibly crossdomain.xml). – yingted Aug 09 '11 at 18:43
  • @chuck: whats your real intention behind doing this, if you could tell us that, there might be some other ways of doing this. are you are doing redirect for unauthorized page?? – Praveen Prasad Aug 10 '11 at 10:29
  • It seems like your best bet would be to route all such requests through an intermediate server, track the redirects and then return the response & endPointURL back to the client-side. That said, if the intended application is entirely client-side that could be impractical. – Daniel Mendel Aug 11 '11 at 00:24
  • @OverZealous Sadly no, this has to be done purely with code inside the JS client. – Chuck Aug 18 '11 at 22:00

3 Answers3

4

So far as I know (and have testet) its only possible to detect IF there has been a redirect and how many redirects were made (but not TO where).

You can have a look my code:

var xhr = $.ajax({
  url: originalURL,
  dataType: "html",
  success: function(data, statusText, jqXHR) {
    console.log(data);
    console.log(statusText);
    console.log(jqXHR.getAllResponseHeaders());
  }
});

The jqXHR.getAllResponseHeaders() output on my dev machine is like that:

Date: Fri, 05 Aug 2011 01:29:20 GMT
Server: ...
X-Powered-By: ... 
Content-Length: 5 
Keep-Alive: timeout=15, max=98 
Connection: Keep-Alive 
Content-Type: text/html

The Keep-Alive: timeout=15, max=98 is worth to have a deeper look at. No redirect result in a max=99 while ONE redirect results in a max=98

scube
  • 1,931
  • 15
  • 21
  • This was a redirect on the same server? I suspect that may be the server decrementing its available number of connections for Keep-Alive support. A redirect to a different server, or interaction with a server that does not have Keep-Alive support, probably won't be detectable with this method. I may very well be wrong, though...I am just reading into Keep-Alive. – JAAulde Aug 05 '11 at 02:17
  • @JAAulde: Yes the tested redirect was on the same server. I don't know what the result will be with different servers. – scube Aug 05 '11 at 07:01
1

XMLHttpRequest.responseXML is a document meaning that it has a baseURI property which will be the location that the data was downloaded from. The main problem is that responseXML will only be set if you get an XML document back. In Firefox using overrideMimeType() works, despite reporting a syntax error in the document:

var r = new XMLHttpRequest();
r.open("GET", "http://google.com");
r.overrideMimeType("text/xml");
r.onload = function()
{
  alert(r.responseXML.baseURI);
}
r.send(null);

Unfortunately, in Chrome you need a real XML document, overrideMimeType() doesn't help. And MSIE doesn't even implement this method (which isn't a big deal given that determining document source there seems impossible).

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • This is close, but doesn't quite get me there. Especially because cross-browser compatibility is actually vaguely important for my case. – Chuck Aug 18 '11 at 21:58
0

I'm not sure what magic they're using server side, but www.longURL.com does what you're talking about.

Their code sends a request to their server:

They have a jquery plug in: http://plugins.jquery.com/project/longurl

I'm not sure how to get the intermediate steps from it, but their website includes the redirects, so they must have figured out some way of doing it, which means they likely have some way of getting that.

To use it you'll have to look at their jquery plugin to figure out where they request the actual data.

EDIT

Allow me to correct that abysmally inadequate answer:

http://www.longURL.com has a service that expands shortened URLs.

Their main website (upon expanding a URL) tracks every redirect until you reach your final destination.

If you did whatever they were doing you might be able to do the same.

Unfortunately I don't know what they're doing (apart from sending their request to a server that probably listens specifically for 303s).

Their jQuery plugin may or may not be useful. If it exposes the redirects and you could figure out how to jimmy rig the system you might be able to get it through their service, otherwise you could create a shortened link to the initial link and get the results through their service anyway...sounds painful, but if you're unable to/unwilling to do server stuff, then that's probably your best option.

Chris Pfohl
  • 18,220
  • 9
  • 68
  • 111