I need to make a series of Ajax requests in a loop. About 100 of them. And each request returns a JSONP variable. I am extracting data from JSON and adding the value to the div. The problem is that I want the div to be added with the data in the order of the function call. sequentially. Now I get a different order every time I refresh the page depending on the order in which the request is executed. Here is my code.
$.each(elem, function (index, item) {
$.ajax({
type: 'post' ,
url: moviesSearchUrl + '&q=' + encodeURI(item) + '&page_limit=1',
dataType: "jsonp",
async: false,
success: searchCallback
});
function searchCallback(data) {
var movies = data.movies;
var markup = index + ': '+ movies[0].title + '<img class=" bord" src="' + movies[0].posters.thumbnail + '" /><br/>';
$("div.content").append(markup);
}
});
});
As I show the index value inside the div, I get random orders every time. 2 4 3 1 7 sometimes and 1 5 2 7 4 sometimes. I even try async: false. It does not help. I read somewhere that JSONP cannot be executed using async: false. Please help me.
source