7

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: Not Modified. I tried to add headers 'Content-Type': 'application/json and 'Accept': 'application/json' but then it returns error 404: Not found.

Here is the code where I am fetching my JSON:

import React from 'react';

export default class LabExperiment extends React.Component {

componentWillMount() {
    const readmePath = require("./docs/iat/iatDependency.json");

    fetch("./docs/iat/iatDependency.json")
    .then(response => {
        response.json();
    })
    .then(text => {
        console.log(text);
    });
}

render() {
    return(
        <div>Hello</div>
    )
  }
}

Here is my json file:

{
    "js": [
        "../jsPsych/jspsych.js",
        "../jsPsych/plugins/jspsych-iat-image.js",
        "../jsPsych/plugins/jspsych-iat-html.js",
        "../jsPsych/plugins/jspsych-html-keyboard-response.js"
    ],
    "css": [
        "../jsPsych/css/jspsych.css"
    ]
}

I also at one point tried to use response.text() instead of json() but it returned my index.html file.

I have been looking around the web for the past week trying to find a solution but what worked for others didn't work for me. Any advice?

EDIT: When I did console.log(response.headers.get('Content-Type')) it returned text/html; charset=UTF-8. Shouldn't it already be application/json? Why is the initial encoding 'text/html'?

kristiyip
  • 95
  • 1
  • 1
  • 7
  • It looks like somehow you are getting an html file instead of json – Prakash Sharma Nov 18 '17 at 17:13
  • Yes, I know that. I have been trying to figure out why this is happening. – kristiyip Nov 18 '17 at 17:17
  • If you're using Express, try `res.json(myJson)`. http://expressjs.com/en/api.html#res.json – lux Nov 18 '17 at 17:54
  • That also just returns an error `TypeError: Cannot read property 'prototype' of undefined`. – kristiyip Nov 18 '17 at 18:50
  • 304 means page is unchanged since you last checked. (So if you clear the cache it shouldn't 304 on the next attempt, but will again after that.) I don't know why `fetch` mis-handles this. I suspect the body explains the 304, like they sometimes do with 404 etc. Try `fetch("./docs/iat/iatDependency.json", {cache: ... })`, using values listed for `cache` at https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch . Also, a 304 response shouldn't have a body according to https://tools.ietf.org/html/rfc7232#section-4.1 ; perhaps that has confused it. – David Knipe Nov 18 '17 at 20:59
  • I'm getting `application/json` as my content type, and it works when developing on localhost, and I've confirmed the file is accessible online by viewing source, but I still get `Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0` – Justin Jan 13 '21 at 08:42

2 Answers2

1

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.

luc
  • 169
  • 1
  • 7
-2

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))

Sahil Raj Thapa
  • 2,353
  • 3
  • 12
  • 23