How does JS update global variables with asynchronous AJAX?

the global variable failed to be assigned after AJAX got the return value. How to solve this problem?

The

code is as follows. Failed to assign values to words [I] and exps [I]

var index = 0;
var words = new Array();
var exps = new Array();

function addWords(shift){
    var add = new XMLHttpRequest();
    add.onreadystatechange = function(){
        if (add.readyState == 4 && add.status == 200){
            var json = eval( "(" + add.responseText + ")" );
            var i;
            for (i in json.items){
                words[i] = json.items[i].word;
                exp[i]= json.items[i].exp;
            }
        }
    }
    add.open("GET", "get.php", true);
    add.send();
}
Apr.11,2021

the first sentence in the addWords function defines var _ this=this;

then words [I], exp [I] changed to _ this.words [I], _ this.exp [I]

Menu