How to get final url after Ajax redirect?

I am working on an AJAX script that makes a GET request to an abbreviated URL, such as a bit.ly link. How can I get the URL of the final / redirected page?

I can get response headers from a redirected page, but it does not contain URL information:

$.ajax({ url: "http://www.thislinkredirects.com", success: function(data, status, xhr) { var response = $(data); }, complete: function(xhr) { console.log(xhr.getAllResponseHeaders()); } }); 

Result:

 Date: Tue, 23 Jun 2015 03:22:07 GMT Allow: POST, GET, OPTIONS, PUT, DELETE Server: lighttpd/1.4.35 X-Powered-By: PHP/5.3.3 Transfer-Encoding: chunked Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE Content-type: text/html Access-Control-Allow-Origin: * Access-Control-Allow-Headers: accept, authorization 

I have no control over the server making the receive request.

+6
source share
4 answers

As I answered a similar question here: fooobar.com/questions/397421 / ...

The best solution is to provide jQuery ajax using a special xhr object as follows:

 var xhr = new XMLHttpRequest(); $.ajax({ url: '/url', type: 'post', data: '...', xhr: function() { return xhr; } }); 

Then you can access the current url in any callback

 success: function () { console.log(xhr.responseURL); } 
+5
source

Try passing jqXHR to ajaxComplete and then read the header.

The title should show the Location: field.

0
source

Try this code .....

 $.ajax({ url: "https://instagram.com/oauth/authorize/?client_id=e40f15a90**c89851d8a66&redirect_uri=http://examle.com&response_type=token", success:function(result,status,xhr){ console.log(xhr.getResponseHeader('Location'));<---NULL } }); 
0
source

Fortunately, I had a canonical tag:

 var matches = xhr.responseText.match(/<link rel="canonical" href="([^"]+)"\/>/); var url = matches[1]; 
0
source

All Articles