I have a little AJAX function that asks the server whether a particular checkbox should be checked. I’d like to pass the information to a variable outside of the scope of the AJAX function. Something along the lines of:
isChecked = $.ajax({
type: "POST",
url: "/ajax/subscribe-query/",
data: "selfKey=" + commentData['selfKeyValue'],
success: function(isSubscribed){
if(isSubscribed == 'true'){
return = true;
}
else{
return = false;
}
}
})
or
var isChecked;
$.ajax({
type: "POST",
url: "/ajax/subscribe-query/",
data: "selfKey=" + commentData['selfKeyValue'],
success: function(isSubscribed){
if(isSubscribed == 'true'){
isChecked = true;
}
else{
isChecked = false;
}
}
})
Neither of those works of course. How do I do this?
in this code even if the ‘isChecked’ property is set properly in the ajax success function the alert will say
undefinedbecause the ajax call isAsynchronous. It will raise the alert before the ajax success function returns. Therefore you need to do your work after the ajax success function like this. You can pass the variable to do the work after ajax success.