J'ai un fichier à une variable comme celui-ci.Comment obtenir des données JSON et les stocker dans une variable.
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},
],
Maintenant, je voulais charger ces données à partir du json. J'ai placé ces données dans le fichier JSON et en utilisant ce code.
getGeography function(){
var geography;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "data.json",true);
}
Maintenant, comment stocker dans une variable et retourner cela.
Utilisez [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