0

Possible Duplicate (Call multiple ajax calls).

I'm trying to use jQuery/AJAX to load to an array. This is my code that works perfectly for one AJAX call.

    $.ajax({
        url: "/Home/ex",
        data: dataEx,
        success: function (result) {
            for (i = 0; i < result.length; i++) 
                arr[i] = new arrayClass(result[i]);
            //arr is an array of arrClass objects
        },
        error: function (error) {
            $("#divError").html(error.responseText);
        },
        dataType: 'json'
    });

Now lets say I want to load in dataEx1, dataEx2, and dataEx3. I have tried multiple solutions, the following is one of many that did not work.

    var list = ["1", "2", "3"];
    var i = 0;
    var runNextAjax = function () {
        var data = "dataEx" + list.pop();
        $.ajax({
            url: "/Home/Ex",
            data: data,
            success: function (result) {
                for (i; i < (result.length + i); i++)
                    arr[i] = new arrayClass(result[i]);
                if (list.length > 0)
                    runNextAjax();
            },
            error: function (error) {
                $("#divError").html(error.responseText);
            },
            dataType: 'json'
        });
    }
    runNextAjax();

I understand it has to do with how AJAX doesn't execute sequentially. How do I fix this?

Community
  • 1
  • 1
Aaron
  • 11
  • 1
  • [async](https://github.com/caolan/async) is a great library for this :) – A1rPun Jul 14 '14 at 18:42
  • Does the data have to be loaded one after the other? Or can you do it in parallel as long as the right data goes in the right place? Hint: loading in parallel can be a lot faster and a bit simpler to implement. – jfriend00 Jul 14 '14 at 19:00
  • jfriend00, the data does not need to be loaded one after another. Teach me how to do this magic you speak of. – Aaron Jul 14 '14 at 19:43

0 Answers0