简体   繁体   English

jquery ajax调用未按预期返回

[英]jquery ajax call not returning as expected

I'm unsure why this jquery ajax call is failing. 我不确定为什么这个jquery ajax调用失败了。 Basically, I want to authenticate and if the authentication is a success, do something. 基本上,我想进行身份验证,如果身份验证成功,请执行某些操作。

I found this but the answer seems to abbreviated to be much use to me (is assumes you already know how to implement the solution). 我找到了这个,但答案似乎缩写为对我有用(假设你已经知道如何实现解决方案)。 jQuery ajax return value jQuery ajax返回值

Here is my ajax call (I have stripped the fluff for getting the username/password): 这是我的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.";
        }
    });

And I call it in this function: 我在这个函数中调用它:

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

When it returns, it always alerts that authReturn is "undefined". 当它返回时,它总是警告authReturn是“未定义的”。 I suspect it's defining authReturn as undefined because the authenticate function 'finishes' before the ajax call gets back... 我怀疑它将authReturn定义为未定义,因为在ajax调用返回之前,authenticate函数'完成'...

But I'm not sure how to fix this problem. 但我不知道如何解决这个问题。

I suspect I could call separate instead of returning values... (say, in this example, calling the emailEvent function directly in the ajax success function) but that would make the authenticate function specific... and it'd no longer be able to be used for authenticating for other purposes. 我怀疑我可以调用单独而不是返回值...(例如,在这个例子中,直接在ajax成功函数中调用emailEvent函数)但这会使验证函数具体...并且它不再能够用于其他目的的身份验证。

You can use your code but will need a callback. 您可以使用您的代码,但需要回调。 A better way would be look into promises. 一个更好的方法是看看承诺。

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);
        });
}

How about pass in a callback function as another argument of the function authenticate(). 如何将回调函数作为函数authenticate()的另一个参数传递。

So the code changes will be 所以代码更改将是

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.");
        }
    });

Calling the function authenticate will become: 调用函数authenticate将变为:

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM