Let me preface this by saying I am extremely new to AJAX/JS in general. I have an issue with undefined return values from AJAX calls. I've tried following some online tutorials or other answers on stack overflow, but the whole callback function this is not working for me. All i want is the data returned so I can use it elsewhere in my code (specifically plotting some charts). Putting the plotting code within the success block seems tacky, so I would like to avoid doing that.
I would really appreciate any help. My code is below:
function updateMe(callback) {
var count1;
var PointsArray = chart.getSelectedPoints();
var xArray = [];
var yArray = [];
var contributions = [];
var finished = 0;
for(count1 = 0; count1 < PointsArray.length; count1++){
yArray[count1] = PointsArray[count1].id;
};
$.ajax({
type: 'POST',
url: "...",
dataType: "json",
data: {
csrfmiddlewaretoken: '{{csrf_token}}',
'data[]': (yArray),
},
success: function(data){
callCallback(data, callback);
}
});
};
function myCallback(result) {
return result;
}
function callCallback(data, cb){
cb(data);
};
var toParse;
toParse = updateMe(myCallback);
EDIT:
I should add that if i do
x = updateMe(myCallback)
in the console, I get the desired result. Then I can use x.responseText to extract the individual data. However if I do
x = updateMe(myCallback).responseText
I get undefined
EDIT #2 Why this is not a duplicate
The tagged question talks about using callbacks as a way to get deal with the asynchronous nature of AJAX calls. My question asks why my callback functions are not working as expected.