I have a function using jQuery AJAX to return data from a data file. However, I cannot get the function to return the value. Any ideas?
$(document).ready(function () {
function getdata() {
var result = 'default value';
$.ajax({
url: 'data/json/load.json',
method: 'GET',
dataType: 'json',
success: function (response) {
result = response.data;
console.log('this is working and the result is: ' + result);
return result;
}
});
return result;
}
var returndata = getdata();
//I'm trying to return the results from my AJAX call here:
console.log(returndata)
});
2
Answers
You should get familiar with how Promises work, they’re very convenient.
That’s how You could use them in your code: