window.onload = function(){ //var url = "http://localhost:8000/sales.json"; var url = "http://gumball.wickedlysmart.com"; //从JaveScript发出一个HTTP请求, 需要使用XMLHttpRequest对象 var request = new XMLHttpRequest(); request.open("GET", url); request.onload = function(){ if (request.status === 200){ console.log(request.responseText); updateSales(request.responseText); } }; request.send(null); } function updateSales(responseText){ var salesDiv = document.getElementById("sales"); //要把responseText串转换未JSON,可以使用JSON.parse方法,反之使用JSON.stringify var sales = JSON.parse(responseText); for (var i = 0; i < sales.length; i++){ var sale = sales[i]; var div = document.createElement("div"); div.setAttribute("class", "saleItem"); div.innerHTML = sale.name + " sold " + sale.sales + " gumballs"; salesDiv.appendChild(div); } }