javascript - function that return a value from ajax call request -


this question has answer here:

i want function returns value ajax request. want stop javascript execution until function value return ajax asynchronous request. like:

function myfunction() {     var data;     $.ajax({         url: 'url',         data: 'data send',         success: function (resp) {             data = resp;         },         error: function () {}     }); // ajax asynchronus request      return data; // return data ajax request } 

you need register callback function, this:

function test() {     myfunction(function(d) {         //processing data         console.log(d);     }); }  function myfunction(callback) {     var data;     $.ajax({         url: 'url',         data: 'data send',         success: function (resp) {             data = resp;             callback(data);         },         error: function () {}     }); // ajax asynchronus request      //the following line wouldn't work, since function returns     //return data; // return data ajax request } 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

php - HTTP_REFERER woes: How can I allow access to a specific page, only when a visitor has visited another specific page beforehand? -