Loading local JSON file

前端 未结 23 1596
悲哀的现实
悲哀的现实 2020-11-22 01:28

I\'m trying to load a local JSON file but it won\'t work. Here is my JavaScript code (using jQuery):

var json = $.getJSON("test.json");
var data = e         


        
23条回答
  •  借酒劲吻你
    2020-11-22 01:55

    Found this thread when trying (unsuccessfully) to load a local json file. This solution worked for me...

    function load_json(src) {
      var head = document.getElementsByTagName('head')[0];
    
      //use class, as we can't reference by id
      var element = head.getElementsByClassName("json")[0];
    
      try {
        element.parentNode.removeChild(element);
      } catch (e) {
        //
      }
    
      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.src = src;
      script.className = "json";
      script.async = false;
      head.appendChild(script);
    
      //call the postload function after a slight delay to allow the json to load
      window.setTimeout(postloadfunction, 100)
    }
    

    ... and is used like this...

    load_json("test2.html.js")
    

    ...and this is the ...

    
      
    
    

提交回复
热议问题