I’m struggling in setting a function’s return value to the json response of its ajax call.
function callMe() {
$.ajax({
url: "someJson.php",
type: "GET",
dataType: 'json',
success: function(response){
return response;
}
}
var jsonResponse = callMe();
How get I get jsonReponse to the hold the json response?
This is not possible by default because ajax is asynchronous; therefore, once you send the ajax request, the function continues and does not wait until the ajax call returns before ending the function. It is best to do whatever you need to inside the success of the ajax call.
Edit:
You can make an ajax call synchronous, however, it is not recommended because it can lock up the browser.
jQuery Ajax Info