简体   繁体   English

如何获取$ .ajax()函数中赋值的变量值?

[英]How to get the value of variable assigned in $.ajax() function?

Here is my code. 这是我的代码。 I want to get the "flag", but the "flag" is always "false". 我想得到“旗帜”,但“旗帜”总是“假”。 Even program executes to "flag=true", the "flag" becomes "false" when it's returned. 即使程序执行“flag = true”,“flag”在返回时也会变为“false”。 How can I get the real value from $.ajax()? 如何从$ .ajax()获得真正的价值? Please help me. 请帮我。 Thanks 谢谢

function checkUserName() {
var name = document.getElementById("userName").value;
var namemsg = document.getElementById("userNameMsg");
var flag=false;
if (name == "") {
    flag=false;
} else {
    $.ajax({
        type : "POST",
        url : "/vclub/verify/checkUserName.do",
        dataType : "json",
        data : "userName=" + name,
        success : function(data) {
            if (data == true) {
                flag=true;
            } else {
                flag=false;
            }
        }
    });
}
alert(flag);
return flag;

} }

Since AJAX is asynchronous, you need to use a callback. 由于AJAX是异步的,因此您需要使用回调。 At the moment you receive the AJAX response after you return flag at the end of your code. 在代码结束时返回标志后,您会收到AJAX响应。 The key point is that success is not called before return flag , since the anonymous function attached to success is called only when the response is received from the url . 关键点是在return flag之前不会调用success ,因为只有在从url接收到响应时才会调用附加到success的匿名函数。 Note that since you may or may not run an AJAX request, you still need to use a callback function when you can provide the return instantly (if the name is an empty string). 请注意,由于您可能会或可能不会运行AJAX请求,因此当您可以立即提供返回时(如果名称为空字符串),您仍需要使用回调函数。 In this case, the callback will be executed shortly after flag is set to false. 在这种情况下,将在flag设置为false后立即执行回调。 Otherwise, the checkUserName will likely complete before the AJAX returns. 否则,checkUserName可能会在AJAX返回之前完成。

function checkUserName(callback) {
    var name = document.getElementById("userName").value;
    var namemsg = document.getElementById("userNameMsg");
    var flag=false;
    if (name == "") {
        // Note a callback is needed even if we don't use AJAX (because we might)
        flag=false;
        callback(flag);
    } else {
        $.ajax({
            type : "POST",
            url : "/vclub/verify/checkUserName.do",
            dataType : "json",
            data : "userName=" + name,
            success : function(data) {
                if (data == true) {
                    flag=true;
                } else {
                    flag=false;
                }
                callback(flag);
            }
        });
    }
}

How to use: 如何使用:

checkUserName(function (flag) {
    // Here, the flag variable is available, but will not run until "callback" is called above
    alert(flag);
});

The flag 's value will change when the AJAX request finishes successfully, because the function that you assigned to success attribute of $.ajax function is a callback. 当AJAX请求成功完成时, flag的值将会改变,因为您为$.ajax函数的success属性分配的函数是回调函数。 In fact your alert(flag); 实际上是你的alert(flag); line of code will run before execution of success callback (When flag 's value is not changed yet). 代码行将在执行success回调之前运行(当flag的值尚未更改时)。

If you move alert(flag); 如果你移动alert(flag); to the success callback, you will see it's value is changed. 对于success回调,你会看到它的价值被改变了。

I think you need to move callback(); 我认为你需要移动callback(); line to the function too. 也适用于该功能。

put the async:false , async:false

function loadProcess(processId) {
    var prc;
    $.ajax({
        async: false,
        url: '/processes/getprocess',
        data: {
            'id': processId
        },
        success(result) {
            prc = result;
        },
        error(xhr) { alert(xhr.responseText); }
    });
    return prc;
}

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

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