AJAX功能返回undefined [英] ajax function returning undefined

查看:145
本文介绍了AJAX功能返回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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆