檢測jQuery $.ajax中的重定向?

[英]Detecting a redirect in jQuery $.ajax?

I want to use jQuery to GET a URL and explicitly check if it responded with a 302 redirect, but not follow the redirect.


jQuery's $.ajax appears to always follow redirects. How can I prevent this, and see the redirect without following it?


There are various questions with titles like "jquery ajax redirect" but they all appear to involve accomplishing some other goal, rather than just directly checking the status that a server gives.

關於“jquery ajax重定向”之類的標題有各種各樣的問題,但它們似乎都涉及到實現其他目標,而不僅僅是直接檢查服務器提供的狀態。

5 个解决方案



The AJAX request never has the opportunity to NOT follow the redirect (i.e., it must follow the redirect). More information can be found in this answer https://stackoverflow.com/a/2573589/965648




Welcome to the future!


Right now we have a "responseURL" property from xhr object. YAY!


See How to get response url in XMLHttpRequest?

查看如何在XMLHttpRequest中獲取響應url ?

However, jQuery (at least 1.7.1) doesn't give an access to XMLHttpRequest object directly. You can use something like this:


var xhr;
var _orgAjax = jQuery.ajaxSettings.xhr;
jQuery.ajaxSettings.xhr = function () {
  xhr = _orgAjax();
  return xhr;

jQuery.ajax('http://test.com', {
  success: function(responseText) {
    console.log('responseURL:', xhr.responseURL, 'responseText:', responseText);

It's not a clean solution and i suppose jQuery team will make something for responseURL in the future releases.


TIP: just compare original URL with responseUrl. If it's equal then no redirect was given. If it's "undefined" then responseUrl is probably not supported. However as Nick Garvey said, AJAX request never has the opportunity to NOT follow the redirect but you may resolve a number of tasks by using responseUrl property.

提示:只需比較原始URL和responseUrl。如果它是相等的,則不會給出重定向。如果它是“未定義的”,那么responseUrl可能不受支持。但是,正如Nick Garvey所說,AJAX請求從來沒有機會不遵循重定向,但是您可以使用responseUrl屬性來解決一些任務。



jQuery runs within the confines of your browser's javascript engine. Every resource that is requested through javascript or html elements (link, script, image, audio or video etc) is subject to http redirection. There is unfortunately no way for javascript to gain access of low-level http in the way that you would need.


So sorry, the answer is no.




While the other folks who answered this question are (sadly) correct that this information is hidden from us by the browser, I thought I'd post a workaround I came up with:


I configured my server app to set a custom response header (X-Response-Url) containing the url that was requested. Whenever my ajax code receives a response, it checks if xhr.getResponseHeader("x-response-url") is defined, in which case it compares it to the url that it originally requested via $.ajax(). If the strings differ, I know there was a redirect, and additionally, what url we actually arrived at.


This does have the drawback of requiring some server-side help, and also may break down if the url gets munged (due to quoting/encoding issues etc) during the round trip... but for 99% of cases, this seems to get the job done.


On the server side, my specific case was a python application using the Pyramid web framework, and I used the following snippet:


import pyramid.events

def set_response_header(event):
    request = event.request
    if request.is_xhr:
        event.response.headers['X-Response-URL'] = request.url



 type: "POST",
 url: "http://arudhrainnovations.com/android/testregister.php",
 data: dataString,
 dataType: "json",
 cache: false,
 success: function(result){
 if (result.success==1)
     windows.locations = "index.html";

To redirect the file after the json response from the live server. check for the respone tag success ==1 and put the below code for redirection.

要在來自活動服務器的json響應之后重定向文件。檢查respone標記的success ==1,並將下面的代碼用於重定向。

windows.locations = "index.html";

窗戶。位置= " index . html”;

Please let me know further details at contact@arudhrainnovations.com




粤ICP备14056181号  © 2014-2021 ITdaan.com