I have the following function:
function validateField(query) {
new Request.JSON({
onRequest : function() {
},
onSuccess : function(json){
return true;
},
data : query + '&validate=true',
url : base + 'ninjquiry.php'
}).send();
}
Now onSuccess is definitely firing because I can put a console.log in there and it works, however if I do the following somewhere else:
console.log(validateField(query));
I get “undefined”. Why is this ?
Thanks for any help in advance
- Alex
Ajax is asynchronous. The
onSuccesscallback is called later, after thevalidateFieldfunction has been finished. Thereturn truewill vanish into thin air.You would have to either make your call synchronous – I’m sure MooTools has a setting for it – but that is highly discouraged, as it can freeze the browser.
Or, this is the better alternative, change your code so that everything relevant happens in the
onSuccesscallback.