[英]My Javascript (and Mootools) function is not returning true or false
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: 现在onSuccess肯定会触发,因为我可以在其中放置console.log,并且可以工作,但是如果我在其他地方执行以下操作:
console.log(validateField(query));
I get "undefined". 我得到“未定义”。 Why is this ? 为什么是这样 ?
Thanks for any help in advance 感谢您的任何帮助
Ajax is asynchronous . Ajax是异步的 。 The onSuccess
callback is called later, after the validateField
function has been finished. validateField
函数完成后,稍后将调用onSuccess
回调。 The return true
will vanish into thin air. return true
将消失在稀薄的空气中。
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. 您将不得不使您的呼叫同步-我确定MooTools可以进行设置-但不建议这样做,因为它会冻结浏览器。
Or, this is the better alternative, change your code so that everything relevant happens in the onSuccess
callback. 或者,这是更好的选择,更改代码,以便所有相关的事件都发生在onSuccess
回调中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.