How do I match the results of two nested ajax calls?
An example: the first call gets a list of urls, and the nested one, for each of them, will retrieve the titles of the pages. What happens is that while I run the second call, the first one continues in background and doesn't find a match for my results. Any solution?
Code:
$.ajax({
dataType : "json",
url : queryUrlDoc,
success : function(json) {
queryResults = json.results.bindings;
var title;
var temp = [];
for(var i in queryResults){
var url = queryResults[i]['doc'].value;
$.ajax({
url: url,
type: 'get',
success: function(data) {
title = data;
var item = {
'url' : url,
'title': title
};
temp.push(item);
}
});
}
var titolo;
for(var i in temp) {
uri = temp[i]['url'];
titolo = temp[i]['title'];
//do stuff
}
},
error: function(){
alert("fail");
}
});
Unfortunately, the 2nd 'for' cycle starts even if the first one hasn't finished it's work...