I write the following code to try to get an array from the background using XMlHttpRequest:
var temp = new Array();
var httpRequest = new XMLHttpRequest();
httpRequest.open('POST', url, true);
httpRequest.send(JSON.stringify(choose + "#" + input + "#"));
httpRequest.onreadystatechange = function() {
var message = httpRequest.responseText;
var temps = message.split("#");
for (var i = 0; i < temps.length; i++)
temp.push(temps[i]);
}
console.log(temp[0]);
console.log(temp.length);
But the first time I execute it, it returns me length=0 and temp[0]=undefined; When I execute it the second time, it returns me the correct length and temp[0],it looks like the first time didn't execute at all. What's the problem and how should I deal with it?