Fetching JSON returns error Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 and status code 304: Not Modified

前端 未结 2 581
难免孤独
难免孤独 2020-12-21 05:20

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

相关标签:
2条回答
  • 2020-12-21 05:45

    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:

    1. Run npm run eject

    After that config folder is created at the root of the app.

    1. Go to config/webpackDevServer.config.js.

    2. Then we need to set disableDotRule: false in webpackDevServer.config.js.

          historyApiFallback: {
           // previously disableDotRule: true,
           disableDotRule: false,
          },
      
    3. Keep your json file i.e docs/iat/iatDependency.json in public folder.

    4. Use fetch('/docs/iat/iatDependency.json').then(res => res.json()).then(data => console.log('data', data))

    0 讨论(0)
  • 2020-12-21 06:06

    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.

    0 讨论(0)
提交回复
热议问题