admin管理员组文章数量:1309963
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:
function verifyViite(viite) {
$.get('dataminer.php?question=validateViite&q='+viite, function(data) {
jQuery.globalEval(data);
if(valid == 1) {
return true;
}
else {
return false;
}
});
}
The data
is either valid = 1;
or valid = 0;
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:
function verifyViite(viite) {
$.get('dataminer.php?question=validateViite&q='+viite, function(data) {
jQuery.globalEval(data);
if(valid == 1) {
return true;
}
else {
return false;
}
});
}
The data
is either valid = 1;
or valid = 0;
2 Answers
Reset to default 5You can't return the Callback return value due to the AJAX request being asynchronous.
Execute whatever code calling verifyViite
inside the callback itself, instead of returning true or false.
Alternativly, you can have synchronous AJAX request.
since the function veryViite doesn't return anything, undefined is the expected result. Since its asynchronouse, you would have to give a callback function that would be called on successful ajax call. Something like this;
function verifyViite(viite,cb) {
$.get('dataminer.php?question=validateViite&q='+viite, function(data) {
jQuery.globalEval(data);
if(valid == 1) {
cb(true);
}
else {
cb(false);
}
});
}
and the call would be like;
verifyViite(viite,function(good){
alert(good);
});
本文标签: javascriptajax function returning undefinedStack Overflow
版权声明:本文标题:javascript - ajax function returning undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741853194a2401188.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论