I have a JavaScript method that call JQuery.get() and i want to return value from itto callee function .. the following is the code :
function CheckExistance(userName) {
$.get(
"JQueryPage.aspx", { name: userName },
function(result) {
if (result == "1") {
value = true;
}
else if (result == "0") {
value = false;
}
else if (result == "error") {
value = false;
}
}
);
return value;
}
i want return value; return the value setted in get() function … i know that get() is asynchronous operation anf i want a solution to do that ?
You cannot “return” the value into the called function; because as you’ve realised, the AJAX call is asynchronous. Instead, you need to effectively delay the execution of the code that follows the AJAX call; by placing this code in the
callbackfunction instead.What happens now, is that this code is executed when the AJAX request has completed, and the
valueis available.Then where your function might previously have been:
Should now be: