Here is some code I’d like to execute. I’d like to wait for AJAX response so I can return something from the server. Any way to achieve this?
function functABC(){
$.ajax({
url: 'myPage.php',
data: {id: id},
success: function(data) {
return data;
}
});
//Wait for AJAX (???)
}
var response = functABC();
New, using jquery’s promise implementation:
Nice read e.g. here.
This is not “synchronous” really, but I think it achieves what the OP intends.
Old, (jquery’s
asyncoption has since been deprecated):All Ajax calls can be done either asynchronously (with a callback function, this would be the function specified after the ‘success’ key) or synchronously – effectively blocking and waiting for the servers answer.
To get a synchronous execution you have to specify
like described here
Note, however, that in most cases asynchronous execution (via callback on success) is just fine.