I am trying to use ajax to get information from my server. the page xxx.php is a page with only 123 written on it. However, when I want to return the content of that page, it returns null.
function myFunct(){
$.ajax({
url: 'xxx.php',
success: function(data) {
return data;
}
});
}
var data = myFunct(); //nothing.
Try to use a ajax error handling, for check your response
}
AJAX is asynchronous.
You only get a response some time after the rest of your code finishes running.
Instead, you need to return the value using a callback, just like
$.ajax
does:// it is response to server, and response executed a bit latter that you assign value
You can use in synchronized mode (despite being recommended with async callback to avoid "freezing" of the code).
Using synchronized:
Please note that ajax is 'asynchronous'. So, the response to your server call may not received by the time myFunct() completes execution. You may put the logic of process the data from your server call in the 'success' of ajax.