How to read JSON file with fetch() in javascript?

前端 未结 2 1114
傲寒
傲寒 2020-11-30 10:19

How can I read local JSON file with fetch function in javascript? I have JSON file with some dump data and one function which read JSON file on server. For example :

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 10:29

    There is the very simple Fetch API:

    you use it simply by:

    // Replace ./data.json with your JSON feed
    fetch('./data.json').then(response => {
      return response.json();
    }).then(data => {
      // Work with JSON data here
      console.log(data);
    }).catch(err => {
      // Do something for an error here
    });
    

提交回复
热议问题