Hey! Yeah, the CORS error is a pain when trying to use javascript load json file from desktop directly.
One workaround is to use a local server like `live-server` (npm package) or even Python's `http.server`. Just run it in the folder where your JSON file is, and then `fetch()` will work fine.
FileReader API *can* work, but like you said, it requires the user to manually pick the file every time. Not ideal if you just wanna load it automatically.
Quick example for FileReader:
```js
const input = document.querySelector('input[type="file"]');
input.addEventListener('change', (e) => {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = (event) => {
const json = JSON.parse(event.target.result);
console.log(json);
};
reader.readAsText(file);
});
```
Hope that helps!
One workaround is to use a local server like `live-server` (npm package) or even Python's `http.server`. Just run it in the folder where your JSON file is, and then `fetch()` will work fine.
FileReader API *can* work, but like you said, it requires the user to manually pick the file every time. Not ideal if you just wanna load it automatically.
Quick example for FileReader:
```js
const input = document.querySelector('input[type="file"]');
input.addEventListener('change', (e) => {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = (event) => {
const json = JSON.parse(event.target.result);
console.log(json);
};
reader.readAsText(file);
});
```
Hope that helps!
