No return value using ajax result on success

by saravana 2014-01-09 15:50:03


The trouble is that you can not return a value from an asynchronous call, like an AJAX request, and expect it to work.

The reason is that the code waiting for the response has already executed by the time the response is received.

The solution to this problem is to run the necessary code inside the success with somefunction(). That way it is accessing the output only when it is available.



$.ajax({ url: "ajax-function.php",
data:{"action":"check","val":val},
type:'post',
async: false,
success: function(output){
return output; //cannot return ajax result on success
}
});



You can do it by like this,


$.ajax({ url: "ajax-function.php",
data:{"action":"check","val":val},
type:'post',
async: false,
success: function(output){
some_function(output); //send value to another function
}
});

885
like
0
dislike
0
mail
flag

You must LOGIN to add comments