My AJAX calls are built inside of a for loop. They need to be in order (synchronous). How do I chain them with jQuery?
var array = ['One', 'Two', 'Three'];
var arrayLength = array.length;
for (var arrayCounter = 0; arrayCounter < arrayLength; arrayCounter++) {
var id = array[arrayCounter];
getData(id);
function getData(id) {
$.ajax({
url: 'http://example.com/' + id,
dataType: 'jsonp',
success: function(d) {
var response = d;
console.log(d);
},
error: function() {
alert("ERROR");
}
});
}
}
See Question&Answers more detail:os