views:

118

answers:

3

I'm doing an AJAX download that is being redirected. I'd like to know the final target URL the request was redirected to. I'm using jQuery, but also have access to the underlying XMLHttpRequest. Does anyone know a way to get the final URL?

It seems like I'll need to have the final target insert its URL into a known location in the headers or response body, then have the script look for it there. I was hoping to have something that would work regardless of the target though.

Additional note: I'm asking how my code can get the full url from production code, which will run from the user's system. I'm not asking how I can get the full url when I'm debugging.

A: 

The easiest way to do this is to use Fiddler or Wireshark to examine the HTTP traffic. Use Fiddler at the client if your interface uses a browser, otherwise use Wireshark to capture the traffic on the wire.

Jim Garrison
A: 

One word - Firebug, it is a Firefox plugin. Never do any kind of AJAX development without it.

Activate Firebug and select Net, then perform your AJAX request. This will show the URL that is called, the entire request (header and body) and the entire response (once again, header and body). It also allows you to step through your JavaScript and debug it - breakpoints, watches, etc.

Chris Johnston
A: 

I'll second the Firebug suggestion. You'll see the url as the "Location" header in the http response. It sounds like you also want to get this url in js? If so, you can get it off the xhr response object in the callback (which you can also inspect using FB!). :)

ss ulrey
Thanks for recognizing I need to do this programmaticially. However I don't think the location header will work. The Location header is a header for the redirect response, not the final response. The XHR header's collection would represent the final response, and not the location header. There are no additional HTTP headers I see that have the location.
Frank Schwieterman
This coffee isn't working. Correction: "would represent the final response, and not the redirect response".
Frank Schwieterman
Hmmm, i guess i haven't come across this... When an xhr gets redirected, doesn't it spawn a new xhr? I guess you are saying that it uses the existing and you lose your original response headers?myReq.xmlhttp.getAllResponseHeaders()?Does tcpmon or fiddler show 2 requests?cheers!
ss ulrey
ss ulrey, no it doesn't spawn a new XHR. The original XHR will continue to resolve Location redirects (until the browser decides it has redirected too many times) and the final response will be the "response" according to the XHR.
eyelidlessness