Friday 28 April 2017

What's the right way to load json files in browsers

I have built a node library loading json files like this

var FILE_PATH = path.join(
  __dirname,
  '../data/data.json'
);

function loadMyJsonFile() {
  return JSON.parse(
    fs.readFileSync(
      FILE_PATH,
      constants.DEFAULT_ENCODING
    )
  );
}

The code works fine on server side. I see this error in Chrome console when the function is executed in browser.

Uncaught TypeError: fs.readFileSync is not a function

I googled a bit and I don't think I can use fs in browser code. What's the right way of loading json files in browser?



via LCYSoft

No comments:

Post a Comment