Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

You need to enable javascript to run this app response when try to access local json file in react

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

like image 483
Gmv Avatar asked Aug 06 '18 16:08

Gmv


People also ask

How do I enable JavaScript on Linux server?

To disable it, or check it is enabled, type about:config in the URL field and confirm that you are not scared of dragons. Search for JavaScript:enabled . If it says true in the right hand column, JS is enabled. To change the setting, click on it and then click Toggle.


2 Answers

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.

like image 180
Çağatay Sel Avatar answered Oct 10 '22 09:10

Çağatay Sel


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.

like image 31
AssaultKoder95 Avatar answered Oct 10 '22 07:10

AssaultKoder95