Answers for "fetch json file"

19

fetch api javascript

fetch('http://example.com/movies.json')
  .then((response) => {
    return response.json();
  })
  .then((myJson) => {
    console.log(myJson);
  });
Posted by: Guest on February-10-2020
5

fetch response json or text

const _fetch = async props => {
   const { endpoint, options } = props
   return await fetch(endpoint, options)
   .then(async res => {
      if (res.ok) {
         return await res.clone().json().catch(() => res.text())
      }
      return false
   })
   .catch(err => {
      console.error("api", "_fetch", "err", err)
      return false
   })
}
Posted by: Guest on June-07-2020
0

how to fetch local json file in javascript

Try to use file system. Don't think reading from a JSON file works like that.

const fs = require('fs');
const json_data = require('../services/contributors.JSON');

fs.readFile(json_data, 'utf8', function (err, data) {
  try {
    data = JSON.parse(data)
    for (let i in data){
    console.log('Name:',data[i].name)
    }
  } catch (e) {
    // Catch error in case file doesn't exist or isn't valid JSON
  }
});
Posted by: Guest on March-18-2021
4

fetch suntax

fetch('http://example.com/movies.json')
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    console.log(data);
  });
Posted by: Guest on March-16-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language