檢測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獲取一個URL並顯式地檢查它是否響應了302重定向,但不遵循重定向。

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

jQuery的美元。ajax似乎總是遵循重定向。我怎樣才能避免這種情況,並在不遵循它的情況下看到重定向?

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 个解决方案

#1


74  

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

AJAX請求從來都沒有機會不遵循重定向(例如。,它必須跟隨重定向)。更多信息可以在這個答案中找到https://stackoverflow.com/a/2573589/965648

#2


28  

Welcome to the future!

歡迎來到未來!

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

現在我們有一個來自xhr對象的“responseURL”屬性。耶!

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:

但是,jQuery(至少1.7.1)不直接提供對XMLHttpRequest對象的訪問。你可以這樣使用:

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.

這不是一個干凈的解決方案,我想jQuery團隊將在未來的版本中為responseURL做一些事情。

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屬性來解決一些任務。

#3


11  

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.

jQuery在瀏覽器的javascript引擎范圍內運行。通過javascript或html元素(鏈接、腳本、圖像、音頻或視頻等)請求的每個資源都受到http重定向的影響。不幸的是,javascript無法以您需要的方式訪問低級http。

So sorry, the answer is no.

很抱歉,答案是否定的。

#4


4  

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.

我將服務器應用程序配置為設置一個自定義響應頭(X-Response-Url),其中包含所請求的url。每當我的ajax代碼收到響應時,它都會檢查是否定義了xhr.getResponseHeader(“x-response-url”),在這種情況下,它會將其與最初通過$.ajax()請求的url進行比較。如果字符串不同,我知道有一個重定向,另外,我們實際到達的url。

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.

這確實有缺點,需要一些服務器端幫助,而且在往返期間,如果url被刪除(由於引用/編碼問題等),也可能會崩潰。但在99%的情況下,這似乎能完成任務。


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

在服務器端,我的具體案例是使用金字塔web框架的python應用程序,我使用了以下代碼片段:

import pyramid.events

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

#5


-11  

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

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

詳情請登錄contact@arudhrainnovations.com


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/02/07/7301c3756cb5383234427890e615521d.html



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