My application performs multiple ajax requests, to do this I call a function 'send' with a set of parameters. On success I still want to use the original set of parameters however the next ajax request may have changed these, which will result in errors. see the code below.
Example: If request A calls the send function with an index of 0, I want that index to still be 0 when i use it in the success block. however by the time Request A is successful request B has changed it to 1 for its own ajax call.
can anyone recommend the best way to maintain the original value corresponding to each request and stopping the change?
code that calls 'send' note that the iterator i is the index I mentioned in my example
makeGithubRequest: function (url, callback, action) {
//if not a stat api dataset then perform one manual call
if(action == "commit" || action == "star"){
darwin.githubModule.send(url[0] + darwin.projectManagerModule.getcurrRequestPage(), callback, 0, action);
} else {
//if a stat api then loop each url, only send true callback on final url
for(i=0;i<url.length;i++){
//only perform actually call back when all request data collected
if(i==(url.length-1)){
darwin.githubModule.send(url[i] + darwin.projectManagerModule.getcurrRequestPage(), callback, i, action);
} else {
darwin.githubModule.send(url[i] + darwin.projectManagerModule.getcurrRequestPage(), darwin.projectManagerModule.noCallBack, i, action);
}
}
}
},
the 'send code'
var darwin = darwin || {};
darwin.githubModule = (function() {
return {
send: function (url, callback, index, action) {
$.ajax({
dataType: 'JSON',
type : "GET",
url : url,
headers : {
Accept: "application/vnd.github.v3.star+json"
},
beforeSend: function(req) {
req.setRequestHeader('Authorization', 'Basic ' + btoa('xxxxx'));
},
success : function(response) {
darwin.Mediator.performSuccessAction(action, response, callback, index);
},
error: function() {
$('#ajaxGetUserServletResponse').text("An error occured when connecting to the API, make sure the url is correct");
$("#ajaxGetUserServletResponse").css({"opacity":"1"});
}
});
}
};
})();