Warner Warner - 5 months ago 11
Ajax Question

how to get request data in ajax success callback method

I do ajax request in foreach and question is that how to get request data in success callback or how to match response data and request.
Thank you in advance!

Answer

Something like this:

var makeRequest = function(data){
    doAjaxRequest(data, function(dataFromServer){
        console.log(data);
    });
}
array.forEach(function(element){
    makeRequest(element.getData());
}

What this does is for each element in array, the variable data is different because it refers to the local scope of the function makeRequest. This way, each callback refers to the proper data.