我有一個這樣的變量文件。如何獲取JSON數據並存儲到變量中。
var geography = [
{ id:"Country", header:"", width:150},
{ id:"Capital", header:"Capital", width:150},
{ id:"Falg", header:"Falg", width:150},
{ id:"Language", header:"Language", width:150},
{id:"Population", header:"Population", width:150},
],
現在我想從json加載這些數據。我將這些數據放入JSON文件並使用此代碼。
getGeography function(){
var geography;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "data.json",true);
}
現在從這裏如何存儲到一個變量並返回。
使用[onreadystatechange的(https://開頭developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/onreadystatechange)ie'xmlhttp.onreadystatechange = function(){ if(xmlhttp.readyState === XMLHttpRequest.DONE && xmlhttp.status === 200)geography = xmlhttp.responseText; } };' – Satpal