I know many topics on this subject already exist (eg How do I return the response from an asynchronous call?) but currently I'm in a state of information overload and I can't seem to get it done. Many topics say to use synchronous requests but since this is not desirable (and by the way not supported anymore by my browser) so I must avoid this.
Problem:
I have a list. I want to pass each item to an ajax request to collect some additional information. The result should be a new list with the additional data.
var list=['a','b','c','d'];
var newlist=[];
for(element in list)
{
var item=list[element];
$.ajax({
url: "process.php",
type:"get",
data:{content:item}
}).success(function(response)
{ newlist.push([item,response]);}
}};
}
I figured now I should have a list like
newlist=[ ['a','response'],['b','response'],['c','response'],['d','response'] ];
(with 'response' being the actual response of course). However, this is not the case. When I place an alert in the success function to display the item it shows 'd' four times.
What am I doing wrong?