How to parse a JSON file without web request or web server?

前端 未结 5 844
旧巷少年郎
旧巷少年郎 2021-01-21 21:08

Looking to build a work-around of the following.

$.getJSON(\'myfile.json\', function (data) {
    showAll(data);
});

I want to avoid using a we

5条回答
  •  孤独总比滥情好
    2021-01-21 21:17

    If you have a permission correctly, you can get the file using

    in favs.js:

    var favs = [
        { url: 'http://google.com' },
        { url: 'http://stackoverflow.com' }
    ];
    

    in script.js:

    console.log(favs); // you can use favs as global variables
    

    Otherwise, if you want to use ajax call such as $.getJSON(), you should have a webserver somehow.


    Additionally, you can load js file dynamically. May you can use the code below for example:

    function loadScript(path, onload) {
      var script = document.createElement('script');
      script.type = 'text/javascript';
      script.src = path;
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(script, s);
      if(onload) s.onload = onload;
    }
    

提交回复
热议问题