📜  使用 javascript 代码示例将 JSON 信息获取到 html 控件中

📅  最后修改于: 2022-03-11 15:03:30.496000             🧑  作者: Mango

代码示例1
/* I put your JSON into an external file, loaded from github */
var url = "https://raw.githubusercontent.com/mspanish/playground/master/jessica.json";

/* this tells the page to wait until jQuery has loaded, so you can use the Ajax call */

$(document).ready(function(){
  $.ajax({
    url: url,
    dataType: 'json',
      error: function(){
        console.log('JSON FAILED for data');
      },
    success:function(results){
  /* the results is your json, you can reference the elements directly by using it here, without creating any additional variables */
  
      var cartItemsList = document.getElementById("cartItemsList");

      results.basket.productList.forEach(function(element) {
      cartItemsList.insertAdjacentHTML( 'beforeend',"
  • " + element.product.name + " : " + element.price+ "
  • "); }); // end of forEach } // end of success fn }) // end of Ajax call }) // end of $(document).ready() function