Determining what jQuery .ajax() resolves a string

2019-02-09 07:31发布

问题:

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)

回答1:

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



回答2:

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



回答3:

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.