9

No Duplicate Question

This question is not a duplicate of one of the above mentioned, since I have no control over the server response as it is the case in the other two questions above.


I use $.get to load the content of an external document into the current website.

However, I need the final URL of this external document. In the case, where the original URL gets redirected (302) to a different URL, I need the new URL.

Can I get the final URL from the loaded document (after 302 redirect) using the jQuery $.get method?


Update

Based on the feedback below, I updated my code to this, but I still don't get the final URL:

$.get(url, function(html, status, xhr){
    console.log(xhr.getResponseHeader('TM-finalURL')); // result: null
});

Logging all response headers with xhr.getAllResponseHeaders() gives me (for a page with 302 redirect) the following result:

Pragma: no-cache
Date: Fri, 28 Feb 2014 15:30:22 GMT
Server: Apache
X-Powered-By: PHP/5.3.28
Transfer-Encoding: chunked
Content-Type: text/html
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection: Keep-Alive
Keep-Alive: timeout=15, max=100
Expires: Thu, 19 Nov 1981 08:52:00 GMT

But no final URL. Did I understand something wrong here?

Simon Ferndriger
  • 4,455
  • 6
  • 28
  • 53
  • 1
    check using: `success: function(data, status, xhr) { console.log(xhr.getAllResponseHeaders()); }` then try: `xhr.getResponseHeader(key)` I'm not sure which 'key' should be targeted for a redirection – A. Wolff Feb 28 '14 at 10:29
  • 1
    ^^ that - `jqXHR.getResponseHeader("TM-finalURL")` (I believe) – Reinstate Monica Cellio Feb 28 '14 at 10:30
  • @Archer no, my bad, didn't read enough your previous comment – A. Wolff Feb 28 '14 at 10:33
  • possible duplicate of [How to manage a redirect request after a jQuery Ajax call](http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call) – Matteo Tassinari Feb 28 '14 at 13:24
  • @A.Wolff Unfortunately, all I get in this case looking at all response headers is this: Date: Fri, 28 Feb 2014 15:19:35 GMT Server: Apache Connection: Keep-Alive X-Powered-By: PHP/5.3.28 Transfer-Encoding: chunked Keep-Alive: timeout=15, max=100 Content-Type: text/html – Simon Ferndriger Feb 28 '14 at 15:20
  • @Archer All I get for "TM-finalURL" (which sounds very promising by the way) is `null` - so, it is not working. Or is it only working in case of a redirect? – Simon Ferndriger Feb 28 '14 at 15:23
  • Hmmm... if it's not in the response headers then it will return null, so no surprise there. I don't suppose you have example urls I could try do you? (One to call it from and one to call) – Reinstate Monica Cellio Feb 28 '14 at 15:25
  • possible duplicate of [Find out where Jquery ajax request gets redirected to](http://stackoverflow.com/questions/3200187/find-out-where-jquery-ajax-request-gets-redirected-to) – Quentin Feb 28 '14 at 15:36
  • @Archer So, it depends on the server and maybe also client if the information is available? Do you have an example which provides this info? – Simon Ferndriger Feb 28 '14 at 15:38
  • By the way: it is NOT a duplicate question of any other, since I CANNOT influence the response headers from the page I'm loading with jQuery. – Simon Ferndriger Feb 28 '14 at 15:40
  • 3
    Then you have no way to detect the redirect. It is handled completely transparently to JavaScript. – Quentin Feb 28 '14 at 15:42
  • @Quentin OK, thank you. Why do you only post comments and not answers? – Simon Ferndriger Feb 28 '14 at 15:49
  • Sorry - I was hoping there was some way of getting the URL of the response, which would obviously be different from the request URL if there had been a redirect of any kind. – Reinstate Monica Cellio Feb 28 '14 at 16:06
  • 1
    @SimonFerndriger As the others have stated, it's not possible, as it happens way before javascript is ever notified. Usually this is solved by detecting server side, that it is a ajax call, and send a header out to tell it to redirect. If it is a single page that it is always redirected to(think login page), you can do a very dirty fix, and check the return content for some determinant, that it is now on the login page. But I do NOT recommend this. – André Snede Mar 03 '14 at 09:18
  • I don't know if you can use it to resolve the issue, but jQuery's `ajax()` method has the `statusCode` callback. This can be used to specify different actions for different HTTP status codes. It sounds like that would be worth investigating for a possible solution. – Matthew Daly Mar 03 '14 at 09:44
  • Can you share a url which gives you REDIRECT response? – miszczu Mar 03 '14 at 10:39
  • > Can I get the final UR... this is real question. you should change your's quest header – Vasilii Suricov Feb 12 '19 at 18:14

1 Answers1

11

Looking at the XHR specification, you can see that it is not built to be flexible. All redirects are handled transparently:

If the response has an HTTP status code of 301, 302 ... transparently follow the redirect

No events are ever triggered in JavaScript with the intermediate status codes. JavaScript will only be notified of the final HTTP status and the data returned, as this is what is supposed to be made available:

Once all HTTP headers have been received, the synchronous flag is unset, and the HTTP status code of the response is not one of 301, 302, 303, 307, and 308

krisk
  • 6,957
  • 1
  • 18
  • 30
  • Thank you so much for clearing this up once and for all. – Simon Ferndriger Mar 05 '14 at 11:48
  • I have found something from where i am unsure if this contains the whole idea behind the original question.I am also not sure if this script is based from a cliënt or external server.However i want to share to you what i found.If it is not compliant at all, just ignore. https://github.com/olalonde/follow-redirects/ – HenryW Mar 09 '14 at 12:31
  • @HenryW, that's a Node.js module, so it runs only server side. – krisk Mar 09 '14 at 16:00