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

前端 未结 5 779
旧巷少年郎
旧巷少年郎 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:19

    Simple, fast, but bad for real project solution:

    1. Rename myfile.json to data.js (name doesn't matter).
    2. Create a variable in data.js and initialize it with your json var myData = {...your json...}
    3. Add to your html file.
    4. Now you can use myData variable from javascript with all data.

    This solution is bad because you add a new variable in global scope and browser would still make a http request to get this .js file.

    Also, If you want to make ajax requests to your local files, you can use http server. Take a look at very simple node js http-server.

提交回复
热议问题