I’m having trouble getting the value from this function, I’m not sure why but is returning empty. I debug it with firebug and it run two times, the first one return empty and then the second one return the value.
Any idea how I can correct this.
function validation() {
if (val()); {
alert("Error");
} else {
alert("Pass");
}
}
function val() {
var answer;
dojo.xhrGet({
url: "ValodS?option=12",
handleAs: "text",
load: function (response) {
if (response == 'Pass') {
answer = false;
} else {
answer = true;
}
}
});
return answer;
}
The problem is that you’re returning answer before it is ever being sent (the AJAX request is run in the backround without blocking the calling code.
The easiest solution would be to have the caller pass a callback function to your function. You could then call the callback function inside of the
loadhandler (thus passing the data that way).