0

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();
}
John M
  • 1
  • 1
  • 1
    `html += getData();` - Well, the `getData` function returns a jqXHR object, not a string. – David Jan 08 '18 at 14:29
  • Great so how would i convert that to be able to read the object or change it to a string, appreciate the quick answer! – John M Jan 08 '18 at 14:32
  • 1
    There's really no string representation of a jqXHR object. If you're looking to get the value from `callback(data);` then you're already storing that in a variable called `eee`. That variable, wherever it is, will be overwritten with each new response from the AJAX requests. If there's anything else you want to do with that value in those callbacks, that's exactly where you'd do it. The linked duplicate has considerably more information about handling responses in asynchronous operations. – David Jan 08 '18 at 14:34
  • Awesome, thank you! – John M Jan 08 '18 at 14:36

0 Answers0