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?