开发者

How to get final destination URL from AJAX?

开发者 https://www.devze.com 2023-02-09 01:24 出处:网络
When I do an XMLHttpRequest, I always get redirected automatically t开发者_运维百科o the URL (presumably by the headers of the response). For example, if I query \"http://www.stackoverflow.com\" I wil

When I do an XMLHttpRequest, I always get redirected automatically t开发者_运维百科o the URL (presumably by the headers of the response). For example, if I query "http://www.stackoverflow.com" I will be redirected to "http://stackoverflow.com".

How can I get that final URL? (http://stackoverflow.com/ in the example)

I checked in the response headers but I cannot seem to find it. (I just used the GET/POST method not HEAD).


Look for a location header in the response.

In the example you gave, accessing www.stackoverflow.com and being redirected to stackoverflow.com here is most definatly a Location header being used.

[trcjr@rigel ~]$ curl -I http://www.stackoverflow.com
HTTP/1.1 301 Moved Permanently
Content-Length: 148
Content-Type: text/html; charset=UTF-8
Location: http://stackoverflow.com/
Date: Sat, 05 Feb 2011 21:47:17 GMT

[trcjr@rigel ~]$ 


Using jQuery (this is for current page url) :

$(document).ready(function () {
        var href = window.location.href.toString();
});

EDIT : For, response page final url, pass jqXHR to ajaxComplete & then read the header.

0

精彩评论

暂无评论...
验证码 换一张
取 消