Basically I am trying to call a function (function 1), that get the results from another function (function 2). And this function 2 is making an ajax call.
So the code would be like this:
function f1 () {
var results = f2();
}
function f2() {
$.ajax({
type: 'POST',
url: '/test.php',
success: function(msg){
}
});
}
I know that is I display an alert in the success function, I get the results. But how can we make this result be sended back?
I tryed to do something like this inside function 2 without success:
return msg;
thanks for your help
In theory, this is possible using jQuery’s
async: falsesetting. That setting makes the call synchronous, i.e. the browser waits until it’s done.This is considered bad practice, though, because it can freeze the browser. You should re-structure your script so you can do the relevant things in the
successcallback instead off1. I know this is less convenient than what you do inf1(), but it’s the right way to deal with Ajax’s asynchronous nature.