I am trying to fetch a JSON file but it returns the errors Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 and 304: N
I think you might be using create-react-app
. As in create-react-app
, webpack-dev-server
is used to handle the request and for every request it serves the index.html
. So you are getting
Unexpected token < in JSON at position 0 and status code 304: Not Modified
So to solve this, you can do following:
npm run eject
After that config
folder is created at the root of the app.
Go to config/webpackDevServer.config.js
.
Then we need to set disableDotRule: false
in webpackDevServer.config.js
.
historyApiFallback: {
// previously disableDotRule: true,
disableDotRule: false,
},
Keep your json file i.e docs/iat/iatDependency.json
in public
folder.
Use fetch('/docs/iat/iatDependency.json').then(res => res.json()).then(data => console.log('data', data))
Check Whether You have the .env file with
REACT_APP_API_URL=http://localhost:3001
in it. The file should be in the root folder.