You have access to the variable if you make it by reference. All objects in Javascript are referenced values, just the primitive values aren't (such as: int, string, bool, etc...)
So you can either declare your flag as an object:
var flag = {}; //use object to endure references.
$.ajax({
... //type, url, beforeSend, I'm not able to access flag here
success: function(){
console.log(flag) //you should have access
}
});
Or force the success function to have the parameters you want:
var flag = true; //True if checkbox is checked
$.ajax({
... //type, url, beforeSend, I'm not able to access flag here
success: function(flag){
console.log(flag) //you should have access
}.bind(this, flag) // Bind set first the function scope, and then the parameters. So success function will set to it's parameter array, `flag`
});
successdoesn't magically remove variable from context. Here theflagvariable is even not accessible from ajax option so OP is setting it outside of ajax method scope$flagand the$.ajaxcall are at the same scope then flag will be available (and it is). There's some missing code in your question...