将AJAX调用存储到数组中然后调用它们

时间:2015-10-20 02:32:51

标签: jquery ajax

我有这个片段。我想准备一个AJAX调用数组,然后调用它们。在所有AJAX完成之后,会发生一些事情,例如: UI渲染。我试过但是在AJAX调用之前调用 then()方法。如何解决?

// In a loop I do this.
var deferred = getDeleteResultDeferred(resultId, deleteResultSuccessHandler);
deferreds.push(deferred);

...

function getDeleteResultDeferred(resultId, deleteResultSuccessHandler) {
    var url = getURI("deleteResult");
    var deferred = $.ajax({
        type : "post",
        url : url,
        data : "resultId=" + resultId,
        success: function(response){
            deleteResultSuccessHandler();
        }
    });
    return deferred;
}

// Then I call the AJAXs.
$.when(deferreds).then(function(){
    // UI rendering.
});

0 个答案:

没有答案