Home >Web Front-end >JS Tutorial >Solution to exception when assigning values to global variables using Ajax in JQuery_jquery
After we use JQuery’s Ajax to extract data from the background, we want to assign it to a global variable, but we can’t assign it. Why?
The reason is actually very simple. The Ajax we use is an asynchronous operation, which means that the data has not been extracted when you assign the value. Of course, you cannot assign it, so you only need to change it to a synchronous operation~
Method 1: Set up synchronization before performing Ajax operation
//Then perform your Ajax operation
$.post(address, parameter, function(data, status) {
if (status == "success") {
//Assign value to Global variables
}
else {
alert("wrong");
}
}); ajax