Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out where Jquery ajax request gets redirected to

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'

like image 596
Fishtoaster Avatar asked Jul 08 '10 01:07

Fishtoaster


People also ask

Do AJAX calls follow redirects?

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.

How do I know if AJAX request is running jQuery?

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

Do AJAX requests get cached?

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.


2 Answers

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.

like image 182
Nick Craver Avatar answered Sep 29 '22 15:09

Nick Craver


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">
like image 24
Magnus Smith Avatar answered Sep 29 '22 16:09

Magnus Smith