21

enter image description hereenter image description hereenter image description hereerror message I am trying to access a local .json file using fetch() like

fetch('fakedata.json')
  .then((res) => res.json())
  .then((data) => {
    console.log('data:', data);
  })

But when i saw in the network it is showing

You need to enable javascript to run this app

I check in some other pages but i cant fix this issue. How to fix this issue

13
  • Maybe you have disabled JavaScript in your developer tools? Developer Tools -> Press -> "Disable JavaScript" Commented Aug 6, 2018 at 16:20
  • No. i did not disable javascript Commented Aug 6, 2018 at 16:25
  • What do you mean by "local json file"? And where exactly do you see that error message? Commented Aug 6, 2018 at 16:29
  • Here i am attached error message also. Please check @Bergi Commented Aug 6, 2018 at 16:36
  • 1
    Possible duplicate of How to disable JavaScript in chrome developer tools Commented Sep 13, 2018 at 1:21

6 Answers 6

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This was my issue too. I had the route that was no longer working directly above this.
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.

Comments

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.

Comments

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)

Comments

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

Comments

-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

1 Comment

This error is happening because when your got the following error, the data is changed to HTML page with "you need..." content. Thats what the user is trying to fix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.