score:-2

It's obvious from the error message in the second image that the file contains invalid JSON data. Precisely, the first character in that file is < which isn't valid in a JSON file.

Unhandled Rejection (SyntaxError): Unexpected token < in JSON at position 0. That means the first character in the file is < which isn't valid.

enter image description here

score:0

I received this message when no proxy to the server was specified inside client package.json file.

"proxy": "http://localhost:5000"

(Assuming the server was setup to listen on port 5000. In my case it also required server restart once added)

score:0

That means, you are not getting anything from your request, it is either your json file does not exist in the directory, or there is some authentication required. . Try to go to the DevTools, select the Network tab, then send your request again. If you see you're endpoint there, hover on it to see if it is pointing to the right directory..

"/fakedata.json" -points to "http://localhost/fakedata.json" "fakedata.json" -points to a directory under "http://localhost" not necessarily a direct sub-directory... Notice the "/" at the beginning.

enter image description here

score:1

I am not sure what you mean by trying to access a local json file. You can not access files on the device that runs the webpage if that is what you mean by local files. You can only access to files that are on the webserver that is serving your applications which are in directory of your application. I assume the second case and assume you have file called fakedata.json at the top directory of your application. In order to access that file you can use import as following:

import myJsonFile from "./fakedata.json"

If you want to use fetch to dynamically load, you should put ./ at the beginning as

fetch('./fakedata.json')

so that it fetches a file called fakedata.json which is located at the root directory.

score:1

I was facing similar issues.

I had put the below code just after app.use

app.get('/*', (req, res) => {
   res.sendFile(path.join(__dirname, '/../', 'build', 'index.html'));
});

which served all the API requests directly.

Maybe this can help others facing this issue.

score:1

May be the file, fakedata.json is in wrong directory. Just move the fakedata.json file from src directory to public directory. Then it should work.


Related Query

More Query from same tag