Struggling with using variables in AJAX. Attempting to have var x store data from var data but it keeps showing as undefined in the console log. Any tips on how to fix this issue? EDIT: The variable x needs to be accessed later in the code, not just for checking the value.
var x;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var results = new Array();
var numberOfSuggestions = 0;
var data = xmlhttp.responseText;
var data = eval(data);
x = data;
/*
some code
*/
}
}
console.log(x);