簡體   English   中英

jquery ajax調用未按預期返回

[英]jquery ajax call not returning as expected

我不確定為什么這個jquery ajax調用失敗了。 基本上,我想進行身份驗證,如果身份驗證成功,請執行某些操作。

我找到了這個,但答案似乎縮寫為對我有用(假設你已經知道如何實現解決方案)。 jQuery ajax返回值

這是我的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調用返回之前,authenticate函數'完成'...

但我不知道如何解決這個問題。

我懷疑我可以調用單獨而不是返回值...(例如,在這個例子中,直接在ajax成功函數中調用emailEvent函數)但這會使驗證函數具體...並且它不再能夠用於其他目的的身份驗證。

您可以使用您的代碼,但需要回調。 一個更好的方法是看看承諾。

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

如何將回調函數作為函數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.");
        }
    });

調用函數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