So- I've got this ajax request, see-. Blonde, about 6 feet tall, looks like this:
$.ajax({
url: 'http://example.com/makeThing',
dataType: 'html',
type: 'POST',
data: {
something:someotherthing
},
complete: function(request, status) {
console.log("headers=" + request.getAllResponseHeaders(););
}
});
What happens is that the request to '/makeThing' returns a 302 redirect to a second url:'getThing/abc123'. All I want to do is find out what that second url is (programmatically- it'll be different every time, so just checking in firebug doesn't help me). I've tried plumbing the response headers that come back to the 'complete' callback, but that just gives me what came back on the second request.
Constraints: -I have no control over the server this is running on- just the js. -Can switch frameworks if I have to (dojo? prototype?)
Ideally, I'd do some sort of header-only request to /makeThing to find out what the redirect url is by getting just the headers of the initial 302 response.
Failing that (since jquery auto-follows redirects and doesn't seem to have a way to step in between the requests), I get retrieve the final response and use it to somehow get the url from... something? The request object, maybe?
TLDR: Sending an ajax request. Framework auto-follows the resulting 302 redirect. How do I figure out where it redirected to?
EDIT, Clarification: The final url will be different every time- calling 'makeThing' causes the server to create what is thereafter hosted at 'getThing/abc123'
ajax appears to always follow redirects. How can I prevent this, and see the redirect without following it? There are various questions with titles like "jquery ajax redirect" but they all appear to involve accomplishing some other goal, rather than just directly checking the status that a server gives.
You need to attach . ajaxStop() to the document to detect when all AJAX requests get completed. Use global: false option in the AJAX requests if you don't want to detect by . ajaxStop() .
Fact #1 : Ajax Caching Is The Same As HTTP Caching It simply obeys the normal HTTP caching rules based on the response headers returned from the server.
If possible, have your server set a header on those pages (the destination pages, not the redirecting one), for example set a "current-location"
header, then you can do this:
$.ajax({
url: 'http://example.com/makeThing',
dataType: 'html',
type: 'POST',
data: {
something:someotherthing
},
complete: function(request, status) {
var location = request.getResponseHeader("current-location");
}
});
Yes, this is a bit hacky, but since the redirect is handled internally in the XmlHttpRequest object (an event to which you can't access), you don't have much choice.
You can check the spec, this is the intended behavior for XmlHttpRequest:
If the origin of the URL conveyed by the Location header is same origin with the XMLHttpRequest origin and the redirect does not violate infinite loop precautions, transparently follow the redirect while observing the same-origin request event rules.
This may not help your particular situation, but when I had the same problem I noticed the page I ended up on after the redirect had a clue in the HTML:
<meta property="og:url" content="THE_URL_OF_THE_PAGE" />
Some pages have script snippets in them used for adding Facebook commenting, and those can sometimes contain the current URL too. Or even as blatantly as this:
<a href="http://www.facebook.com/sharer.php?u=THE_URL_OF_THE_PAGE">
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