ajax - 返回并阻止deferred.then()执行

时间:2014-11-05 08:10:18

标签: javascript jquery ajax

我有这样的AJAX请求:

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

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

我想运行该回调函数,因此退出success函数中的ajax请求并阻止deferred.then()执行。 在我的情况下,callback被解雇但在此之后deferred.then()也被执行了,我不希望这种情况发生。

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

使用这样的标志:

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

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