19

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

Gmv
  • 2,008
  • 6
  • 29
  • 46
  • Maybe you have disabled JavaScript in your developer tools? Developer Tools -> Press `⋮` -> "Disable JavaScript" – Tholle Aug 06 '18 at 16:20
  • No. i did not disable javascript – Gmv Aug 06 '18 at 16:25
  • What do you mean by "local json file"? And where exactly do you see that error message? – Bergi Aug 06 '18 at 16:29
  • Here i am attached error message also. Please check @Bergi – Gmv Aug 06 '18 at 16:36
  • That's a weird preview for a json file. What do the *Headers* and *Response* tabs show? – Bergi Aug 06 '18 at 16:44
  • for localhost preview also have the same one. please check another attached file @Bergi – Gmv Aug 06 '18 at 16:53
  • @Kallis I am looking for something that is *not* just the preview but the actual raw data. In any case, this looks like your server is broken. – Bergi Aug 06 '18 at 16:55
  • how to fix this @Bergi – Gmv Aug 06 '18 at 17:14
  • @Kallis I can't tell without you posting more information about the server you are fetching from – Bergi Aug 06 '18 at 17:18
  • what are the information you need? @Bergi – Gmv Aug 06 '18 at 17:20
  • 1
    Possible duplicate of [How to disable JavaScript in chrome developer tools](https://stackoverflow.com/questions/13405383/how-to-disable-javascript-in-chrome-developer-tools) – jhpratt Sep 13 '18 at 01:21
  • Are you able to solve this issue? I am facing the same issue and unable to get it how is it happening? @Gmv – DevProf Apr 14 '20 at 11:38
  • @DevProf that was some manual error only. But, cant Remember how i fix that – Gmv Apr 15 '20 at 12:08

6 Answers6

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.

AssaultKoder95
  • 188
  • 1
  • 9
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.

Çağatay Sel
  • 801
  • 7
  • 14
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.

Md Isfar Uddin
  • 692
  • 11
  • 19
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)

MaxWidth
  • 468
  • 3
  • 9
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

nrs
  • 186
  • 2
  • 7
-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

Ahmed Mokhtar
  • 2,388
  • 1
  • 9
  • 19
  • 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. – KpsLok Jun 29 '21 at 09:01