AJAX功能返回undefined [英] ajax function returning undefined
问题描述
我有一个函数来验证code与AJAX(PHP)。然而,当我尝试运行它,它会返回未定义
时,它应该返回真
或假
。
I have a function to validate a code with AJAX(PHP). However, when I try to run it, it returns undefined
when it should return true
or false
.
功能:
function verifyViite(viite) {
$.get('dataminer.php?question=validateViite&q='+viite, function(data) {
jQuery.globalEval(data);
if(valid == 1) {
return true;
}
else {
return false;
}
});
}
在数据
是有效期= 1;
或有效期= 0;
推荐答案
您不能在回调返回值返回由于AJAX请求是异步的。
You can't return the Callback return value due to the AJAX request being asynchronous.
执行任何code调用 verifyViite
回调本身中,而不是返回true或false。
Execute whatever code calling verifyViite
inside the callback itself, instead of returning true or false.
Alternativly,你可以有<一个href="http://stackoverflow.com/questions/133310/how-can-i-get-jquery-to-perform-a-synchronous-rather-than-asynchronous-ajax-req">synchronous AJAX请求。
Alternativly, you can have synchronous AJAX request.
这篇关于AJAX功能返回undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!