Return data after ajax call success jQuery

The only way to return the data from the function would be to make a synchronous call instead of an asynchronous call, but that would freeze up the browser while it’s waiting for the response.

ajax-jquery

You can pass in a callback function that handles the result:

function testAjax(handleData) {
     $.ajax({
         url:"getvalue.php", 
         success:function(data) {
             handleData(data); 
         }
     });
}

Call it like this:

testAjax(function(output){
  // here you use the output
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s