全局值恢复到原始值后AJAX(jQuery的)全局、原始、jQuery、AJAX

2023-09-11 01:27:36 作者:矜暮

我是比较新的jQuery的,我想解决这个code,但不断失败,我现在面临的问题是,我有一个gloval阵列(Roles_Permission),我可以用这个在我AJAX请求,我也能够做出改变阵列的内容,但现在我出来了,所有的全局值恢复到原有的,为什么呢?!?需要帮忙。请看看下面的code,

I am relatively new to jQuery, I am trying to work around this code but constantly failing, the problem I'm facing is, I have a gloval array (Roles_Permission), I am able to use this in my AJAX request, I am also able to make changes to the Array's content but the moment I come out, all the global values return to original ones, Why?!? Need help. Please look at the following code,

// Global Variable
var Role_Permission = {
    "Value1" : true;
    "Value2" : true;
    "Value3" : true;
}

现在我有一个函数,如下所示,

Now I have a function which is as follows,

function checkRoles(){
    requestData(
        "roleData",
        { roleName: "testUser"},
        function(result){
            Role_Permission["Value2"] = false;
            alert(Role_Permission["Value2"]);
        }
    });

    alert(Role_Permission["Value2"]);
}

AJAX外的警报返回值2的值作为真然而,在它里面返回为FALSE,为什么呢?!?请帮忙

The alert outside AJAX returns value of Value2 as True however, inside it returns as FALSE, Why?!? Please help

三江源

推荐答案

由于AJAX请求异步运行,这就是为什么你给它一个将要调用一旦完成回调。回调外的一切都将被马上甚至当你刚刚发送的请求仍在运行执行。这就是为什么底部警报仍然有旧的价值,但一个回调内部的新的。

Because AJAX request run asynchronously, which is why you are giving it a callback that will be called once it is done. Everything outside the callback will be executed right away even while the request you just sent is still running. This is why the bottom alert still has the old value, but the one inside the callback the new one.