jquery ajax调用未按预期返回

时间:2015-03-16 18:28:17

标签: javascript jquery ajax

我不确定为什么这个jquery ajax调用失败了。基本上,我想要进行身份验证,如果身份验证成功,请执行某些操作。

我找到了这个,但答案似乎缩写为对我有用(假设你已经知道如何实施解决方案)。 jQuery ajax return value

这是我的ajax电话(我已经删除了获取用户名/密码的漏洞):

function authenticate() {
    $.ajax({  //TODO: Securely send this (ssl?)
        type: 'POST',
        url: 'includes/clientAuthenticate.php',
        data: { username: username, password:  password},
        success:function(data){
            if(data==='true') {
                return "true";
            } else {
                return "User failed to log into the system.\nEmail request ignored.\n\nError message: \n" + data;
            }
        },
        error:function(jqXHR, textStatus, errorThrown){
            return "User failed to log into the system. Potential problem with server or connection.";
        }
    });

我在这个函数中调用它:

function attemptEventSubmit(eKey) {
    var authReturn = authenticate();
    if(authReturn=="true") {
        emailEvent(eKey);
    } else {
        alert(authReturn);
    }
}

当它返回时,它总是警告authReturn是"未定义"。我怀疑它将authReturn定义为未定义,因为身份验证功能已完成'在ajax调用回来之前......

但我不确定如何解决这个问题。

我怀疑我可以调用单独的而不是返回值...(例如,在这个例子中,直接在ajax成功函数中调用emailEvent函数)但这会使验证函数具体...并且它会使... d不再能够用于其他目的的身份验证。

2 个答案:

答案 0 :(得分:2)

您可以使用您的代码,但需要回调。更好的方法是研究承诺。

function authenticate(onsuccess, onfail) {
    $.ajax({  //TODO: Securely send this (ssl?)
        type: 'POST',
        url: 'includes/clientAuthenticate.php',
        data: { username: username, password:  password},
        success:function(data){
            onsuccess(data); // you should check if this is a function
        },
        error:function(jqXHR, textStatus, errorThrown){
            onfail(errorThrown);
        }
    });

function attemptEventSubmit(eKey) {
    authenticate(
        function(ajaxData){
            emailEvent('whatever you want to send');
        },
        function(errThrown){
            alert(errThrown);
        });
}

答案 1 :(得分:1)

如何将回调函数作为函数authenticate()的另一个参数传递。

所以代码更改将是

function authenticate(callback) {
    $.ajax({  //TODO: Securely send this (ssl?)
        type: 'POST',
        url: 'includes/clientAuthenticate.php',
        data: { username: username, password:  password},
        success:function(data){
            if(data==='true') {
                //return "true";
                callback("true");
            } else {
                //return "User failed to log into the system.\nEmail request ignored.\n\nError message: \n" + data;
                callback("User failed to log into the system.\nEmail request ignored.\n\nError message: \n" + data);
            }
        },
        error:function(jqXHR, textStatus, errorThrown){
            //return "User failed to log into the system. Potential problem with server or connection.";
            callback("User failed to log into the system. Potential problem with server or connection.");
        }
    });

调用功能身份验证将变为:

function attemptEventSubmit(eKey) {
    authenticate(function(authReturn){
        if(authReturn=="true") {
             emailEvent(eKey);
        } else {
             alert(authReturn);
        }
    });

}