I have an array of statements and I want to loop through each and submit to the server via an ajax call. I have to make sure each ajax request is executed before the next request is sent, because on the server side code, I am creating a unique key for this array of statements. What happens is before I can commit the first request, the second request comes in and I end up have 2 separate keys being created since it sees each of them as a first time request. I can't rely on timeout entirely and the code I have so far, continues with the loop.
function formPostSubmission(form){
var parts = ['a', 'b', 'c'];
var i = 0;
parts.forEach(function(entry) {
i++;
datafield ={
answer:entry,
displayOrder:i,
ajaxName:'statementPartialSubmit'
};
$.when(submitAjaxData(form, datafield)).then(function succesHandler(data){
console.log("promise test "+data);
console.log("Below request ");
},
function errorHandler(){
console.log("Error occurred ");
})
console.log("Go for next ");
});
}
function submitAjaxData(form, datafield) {
console.log(" called submitAjaxData ");
var loadurl = domainName + "/ajax-call-handler";
return $.ajax({
url: loadurl,
method: "GET",
data: datafield
});
}
I was hoping to check response data for a success and then proceed within the loop. But this is how my console prints.
called submitAjaxData
Go for next
called submitAjaxData
Go for next
promise test
Below request
promise test
Below request