admin管理员组文章数量:1277377
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?
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?
Share Improve this question edited May 28, 2014 at 22:09 Simon Ferndriger asked Feb 28, 2014 at 10:22 Simon FerndrigerSimon Ferndriger 4,9626 gold badges33 silver badges62 bronze badges 17-
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 Commented Feb 28, 2014 at 10:29 -
1
^^ that -
jqXHR.getResponseHeader("TM-finalURL")
(I believe) – Reinstate Monica Cellio Commented Feb 28, 2014 at 10:30 - @Archer no, my bad, didn't read enough your previous ment – A. Wolff Commented Feb 28, 2014 at 10:33
- 3 Then you have no way to detect the redirect. It is handled pletely transparently to JavaScript. – Quentin Commented Feb 28, 2014 at 15:42
- 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 remend this. – André Snede Commented Mar 3, 2014 at 9:18
1 Answer
Reset to default 11 +50Looking 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
本文标签: javascriptHow to detect a server redirect with get()Stack Overflow
版权声明:本文标题:javascript - How to detect a server redirect with $.get() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741290976a2370545.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论