4

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

3
  • 1
    stackoverflow.com/questions/6538470/… Commented Sep 7, 2012 at 12:17
  • 2
    @Vishal, that answer doesn' t solve the problem to start an ajax call after the success of the previous one. Commented Sep 7, 2012 at 12:21
  • Ah! didn't saw the success part..Ignore the previous comment, I'll try to work out something. Commented Sep 7, 2012 at 12:25

1 Answer 1

4

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.