I have trouble with using variables in AJAX. I want var x to remember data from var data. However when I check in the console log the value is undefined. Can somebody help me with this? EDIT: I need to use the variable x later in the code. It's not just about 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);