Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
18
rated 0 times [  19] [ 1]  / answers: 1 / hits: 16200  / 15 Years ago, mon, april 20, 2009, 12:00:00

I have a javascript function that calls a generic function to make an ajax call to the server. I need to retrieve a result (true/false) from the callback function of the ajax call, but the result I get is always 'undefined'.



A super-simplified version of the generic function without all my logic would be:



function CallServer(urlController) {
$.ajax({
type: POST,
url: urlController,
async: false,
data: $(form).serialize(),
success:
function(result) {
if (someLogic)
return true;
else
return false;
},
error:
function(errorThrown) {
return false;
}
});
}


And the function calling it would be something like:



function Next() {
var result = CallServer(/Signum/TrySave);
if (result == true) {
document.forms[0].submit();
}
}


The result variable is always 'undefined', and debugging it I can see that the return true line of the callback function is being executed.



Any ideas of why this is happening? How could I bubble the return value from the callback function to the CallServer function?



Thanks


More From » ajax

 Answers
118

Just found how to do it :) Declaring a variable and updating it accordingly from the callback function. Afterwards I can return that variable. I place the code for future readers:



function CallServer(urlController) {
var returnValue = false;
$.ajax({
type: POST,
url: urlController,
async: false,
data: $(form).serialize(),
success:
function(result) {
if (someLogic){
returnValue = true;
return;
}
},
error:
function(errorThrown) {
alert(Error occured: + errorThrown);
}
});

return returnValue;
}

[#99678] Tuesday, April 14, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leog

Total Points: 225
Total Questions: 113
Total Answers: 118

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;