簡體   English   中英

為什么使用$ .deferred和$ .ajax時jQuery.then()的行為不同

[英]Why does jQuery.then() behave differently when using $.deferred and $.ajax

我一直試圖繞過jQuery Deferred對象。 我的目的是檢查每個ajax響應(成功/失敗)。 我想做到這一點而不會干擾其他聲明典型$ .ajax()。done()。fail()請求的代碼。

我已經使用$ .ajaxPrefilter()來獲取每個ajax請求,然后再執行它。 使用jqXHR對象上的.then()方法,我設法添加了一個函數,該函數將在放置在原始$ .ajax()調用上的.done()方法之前被調用。

下面的代碼將打印出以下內容:

def完成
然后定義
然后第二個ajax預過濾器
第二個ajax完成
然后第二個ajax
阿賈克斯完成
然后阿賈克斯

我不明白的是為什么要先執行預過濾器步驟。 我希望它最后執行一次,或者根本不執行。

這是我想要的行為,但是我不明白為什么。

// this is a typical usage of deferred with two done functions added, the second via .then()
var def = $.Deferred();
def.done(function(){
    document.write("def done<br>");
});
def.then(function(){
    document.write("def then<br>");
});
def.resolve();

// this is a typical ajax request with a done function added, followed by another using .then()
$.ajax("/echo/json/").done(function(){
    document.write("ajax done<br>");
}).then(function(){
    document.write("ajax then<br>");
});

// for the third request i intercept and call the .then() method 
$.ajaxPrefilter( 
    function( options, originalOptions, jqXHR ) {
                jqXHR.then(function(data, textStatus, jqXHR){
                     document.write("2nd ajax prefilter then<br>");
                    });
            });

// create a typical ajax request. these will be executed after the prefilter .then()
$.ajax("/echo/json/").done(function(){
    document.write("2nd ajax done<br>");
}).then(function(){
    document.write("2nd ajax then<br>");
});

預先感謝您的任何幫助

更新:------------

在@Bergi響應中,以下代碼演示了如何在done()之前調用$ .ajaxPrefilter()。

$.ajaxPrefilter( 
    function( options, originalOptions, jqXHR ) {
            document.write("prefilter function within $.ajax call<br>");
                jqXHR.then(function(data, textStatus, jqXHR){
                     document.write("2nd ajax prefilter then<br>");
                    });
            });

var functionToRunWhenDoneIsCalled = function() {
    document.write("done is called function<br>");
    return function(){
       document.write("2nd ajax done<br>");
    }
}

$.ajax("/echo/json/").done(
    (functionToRunWhenDoneIsCalled)()
).then(function(){
    document.write("2nd ajax then<br>");
});

輸出:

$ .ajax調用中的預過濾器功能
完成稱為功能
然后第二個ajax預過濾器
第二個ajax完成
然后第二個ajax

這回答了我關於.then()方法如何在.done()方法之前附加到延遲的jqXHR對象的問題。

在你的情況下,與添加回調之間沒有區別.done()或用.then() 僅使用.done()就足夠了。

我不明白的是為什么要先執行預過濾器步驟。 我希望它最后執行一次,或者根本不執行。

回調將按添加到延遲對象中的順序執行。 並且prefilter在$.ajax內部執行,即,甚至在$.ajax調用返回之前,也可以附加回調, then可以附加您的done和handler。

如果您不返回延遲的對象,則所有.then要做的就是向該延遲的對象添加另一個完成的失敗和/或進度處理程序。 考慮到這一點,在預過濾器中添加$.ajax()$.ajax()之后添加的$.ajax()之前執行是完全有意義的.then因為pre-filter回調中的代碼首先發生。 回調先入先出。

我不明白的是為什么要先執行預過濾器步驟。 我希望它最后執行一次,或者根本不執行。

您已經將另一個“要做的事情”附加到了與ajax請求關聯的jqXHR上。 由於它是一個過濾器,因此會在ajax請求使用的標准完成/失敗之前被附加。 處理程序按其連接的順序運行,因此預過濾器是第一個。

請注意,由於預過濾器僅在.then()方法中附加了單個功能,因此.then()如果由於某種原因請求失敗,將不會運行任何內容。 聽起來您也想擁有第二個(故障處理程序)arg。

至於兩個不同的ajax請求的完成順序,這是不可預測的。 這將取決於哪個先返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM