2014-11-05 20 views
-2

我有這樣一個AJAX請求:阿賈克斯 - 回報和防止deferred.then()執行

$.ajax({ 
    type: 'GET', 
    url: url, 
    dataType: "json", 
    success: function(data) { 
     // ... 
     callbak(true); 
    }, 

    }) 
    .then( 
    function(response) { 
     // ... 
    }); 

我想運行從AJAX請求回調函數,因此出口在success功能,防止deferred.then()執行。 在我的情況下,callback被解僱,但之後deferred.then()也被執行,我不希望發生這種情況。

有什麼想法?

感謝

+0

你想只在故障運行'then'回調呢?你爲什麼不使用'error'回調? – gdoron 2014-11-05 08:12:35

+0

@gdoron有沒有寫過的代碼,但我不能使用'error',因爲在某些情況下'data'值是空的,但'success'函數被觸發,我需要退出ajax請求。 – Frank 2014-11-05 08:16:24

+0

你的問題恐怕還是很不清楚。 – gdoron 2014-11-05 08:17:13

回答

0

使用標誌,如下所示:

var executeThen = true; 
$.ajax({ 
     type: 'GET', 
     url: url, 
     dataType: "json", 
     success: function(data) { 
      // ... 
      executeThen = false; 
      callbak(true); 
     }, 

    }) 
    .then(function(response) {   
     if(executeThen){ 
     // ... 
     } 
    });