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)
ajax appears to always follow redirects.
How do I redirect to another view in ajax success? $. ajax({ type: 'POST', url: 'AJAX URL', data: “YOUR DATA” // don't forget to pass your csrf_token when using post success: function(data){ $(“what_ever_you_want_to_replace”). html(data.
It uses readyState property of XMLHTTPRequest; readyState is simply an integer value which describes the status of HTTP request, whenever onreadystatechange function is called when readyState property changes.
How AJAX Calls Work. AJAX uses both a browser built-in XMLHttpRequest object to get data from the web server and JavaScript and HTML DOM to display that content to the user. Despite the name “AJAX” these calls can also transport data as plain text or JSON instead of XML.
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
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With