AJAX and importing JSON file and printing each of the items of json

 function gg(){

    console.log("you clicked the click button")

    let xhr= new XMLHttpRequest();
    xhr.open("GET","load.json",true);
    xhr.onprogress=function(){
        console.log("progress"); 
        //you can add spinner over here
    }
    xhr.onload= function(){
        let obj=JSON.parse(xhr.responseText)
        //document.getElementById("change").innerHTML=JSON.parse(xhr.responseText)
      console.log(obj.fruits.length)
       // console.log(obj.fruits[0])
       let html=''
       for(i=0; i<obj.fruits.length;i++){
           console.log(obj.fruits[i].name)
          html=html+ '<li>'+obj.fruits[i].name+"==>"+obj.fruits[i].price+'</li>'  

       }
       document.getElementById("change").innerHTML=html
        //console.log(Object.keys(obj.fruits[0]));
    }
    xhr.send()


}

Comments