I have an ajax call call that performs within a loop to query a database and bring back information regarding a table I am building. I have tried for a week to return the correct information from the callback function with now with no luck. The code I am using is is below. To make things simpler data.i returns and integer 1-30 before i change it to the actual data i will need in the future (yes i know this can be done just in the for loop but the real data i need later will be changed to a string).
What i need to be able to do is return the formatted td with information from the ajax call and return it outside the callback function to work with the rest of the code that I have and so the function can proceed. I have checked other posts on this site but none seem to give the answer as to how to do this.
If i console log eee i get the correct value of the td cell but this will not pull outside the returned request.
Thanks
for(i = 1; i <= monthLastDay; i++) {
var r;
var vars = {
i: i
}
function getData() {
return $.ajax({
url: "http://localhost:8888/vs-time-tracking-ajax-calendar/",
data: vars,
type: "POST",
dataType: "json",
success: function (data) {
eee = callback(data);
},
error: function (request, status, error) {
alert(request.responseText);
}
})
}
function callback(data) {
console.log(data.i);
if(moment(timeNowLocal).date(data.i).format("D.M.YYYY") === timeNow.format("D.M.YYYY")) {
r = '<td class="ic__day ic__day_state_current">' + data.i + '</td>';
} else if(timeSelected && moment(timeNowLocal).date(data.i).format("D.M.YYYY") === timeSelected.format("D.M.YYYY")) {
r = '<td class="ic__day ic__day_state_selected">' + data.i + '</td>';
} else {
r = '<td class="ic__day">' + data.i + '</td>';
}
return r;
}
//This returns [Object, object]
html += getData();
}