Chaining multiple ajax calls with jquery deferred objects

雨燕双飞 提交于 2019-12-04 14:47:43

问题


While looking for a solution similar to as described here: How to chain ajax calls using jquery I am looking for a solution using jquery v1.52.

I have a set of ajax requests to be made. But each ajax request is to be sent only after completing the previous ajax call. I am trying to achieve this with jquery 1.5.2 but just unable to. Here is what I modified from the above mentioned example. It does not work. Can anyone help me get this working? Expected output is http://jsfiddle.net/k8aUj/3/

P.S: I cannot upgrade to version beyond 1.5.2


回答1:


Yo! Solved! http://jsfiddle.net/sandhyasriraj/AaHZv/

var x = null;
var i = 0;
x= $.Deferred();
var countries=["US","CA","MX","bx","fs","ZX"];
function log(msg) {
    var $out=$("<div />");
    $out.html(msg);
    $("#console").append($out);
}


callX = function(j) {
    return $.ajax({
            type: "GET",
            url: "/echo/json/",
            data: {country:countries[i]},
            dataType: "JSON",
            success: function(){
                log("Successful request for [" + countries[j] + "]");
                i++;      
                x.resolve();
                xy();
               }
        });

}
x.resolve();
xy = function()
{
    debugger;
    if(i > 5)
        return;

     $.when(x).then(function() {
        x = $.Deferred();
        log("Making request for [" + countries[i] + "]");
        callX(i);
    });
}
xy();

is this what you want?



来源:https://stackoverflow.com/questions/12317887/chaining-multiple-ajax-calls-with-jquery-deferred-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!