外面我的JavaScript代碼:JavaScript數組空函數
var buffer=new Array();
function fetchData(min,max){
var ajaxReq = new XMLHttpRequest();
ajaxReq.onreadystatechange = function(){
if (ajaxReq.readyState === 4) {
if (ajaxReq.status === 200) {
buffer= ajaxReq.responseText;
console.log(buffer)//this logs an array to console
} else {
console.log("Error", ajaxReq.statusText);
}
}
};
ajaxReq.open('GET', "server/controller.php?min="+min+"&max="+max, true);
ajaxReq.send();
}
fetchData(1,100);
console.log(buffer);//this log an empty array
兩個日誌有不同的結果,我究竟做錯了什麼?感謝指針。
'fetchData'函數中的'buffer'變量是條件集。你確定'ajaxReq.status'和'ajaxReq.readyState'等於你想要的嗎? –