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

前端 未结 2 582
难免孤独
难免孤独 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))

提交回复
热议问题