jquery - Get returned value from javascript function -


function get_new_project_id() {     function subfunction() {         $.ajax({           url: 'includes/ajax.php?request=create_untitled_project',           success: function(response) {             return response; // result number 19           }         });     }     return subfunction(); };  var resultnumber = get_new_project_id(); 

this 1 of basic questions... why isnt resultnumber 19? how can can return html or text value function? xmlhttp request object. want simple number returned.

that's because success function executed asynchronously because ajax call. makes no sense return value in ajax callback because execute after containing function has returned.

in order fix need manipulate result inside success function because available there:

function subfunction() {     $.ajax({         url: 'includes/ajax.php?request=create_untitled_project',         success: function(response) {             dosomethingwiththeresult(response);         }     });     return 'ajax request initiated results available later'; } 

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -