function get_new_project_id() {
function subFunction() {
$.ajax({
url: 'includes/ajax.php?request=create_untitled_project',
success: function(response) {
return response; // result is a number like 19
}
});
}
return subFunction();
};
var resultnumber = get_new_project_id();
this is probably one of the most basic questions… why isnt resultnumber 19? how can I do it so it can return a html or text value from function? sometimes i get xmlhttp request object. i just want a simple number to be returned.
That’s because the
successfunction is executed asynchronously because it is an AJAX call. So it makes no sense to return a value in an AJAX callback because it could execute much after the containing function has returned.In order to fix this you will need to manipulate the result inside the success function because they will be available only there: