I have a function sendAjax() which makes an Ajax request (with jQuery). How can I return the response from sendAjax()?
I tried to return the value from the success response, as well as assigning the response to a local variable inside the function and return that one, but none of those ways actually return the response.
Code: Select all
function sendAjax(url, datasend) {
var result;
$.ajax({
url: url,
data: datasend,
success: function(response) {
result = response;
// return response; // <- tried that one as well
}
});
return result;
}
var result = sendAjax('file.php', 'id=9'); // always ends up being `undefined`