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"]);
}
The alert outside AJAX returns value of Value2 as True however, inside it returns as FALSE, Why?!? Please help
Thankyou
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.