DWQA QuestionsCategory: ProgramIn react, fetch is used to request JSON data, and HTML page is returned
Too little to eat asked 1 month ago

problem
In the react code, a map component needs to be rendered and a JSON data is requested. It is expected that the JSON data will be injected into the map building after the request is successful. However, what you see in the console is the content in index.html, not the JSON data.
I tried to request another JSON file or a nonexistent JSON file, and what I returned was also the content in index.html
Related code

componentWillMount() {
   fetch('../data/wuhan.json')
   .then(function(response) {
     return response.json();
   })
   .then(function(json) {
     Echarts. Registermap ('wuhan ', JSON); // register map component
   })
   .then(function() {
     This. Updataseries(); // process series
   })
 };

Console error
In react, fetch is used to request JSON data, and HTML page is returned
My project directory structure
In react, fetch is used to request JSON data, and HTML page is returned
View the requested JSON in the console and return the contents of index.html in the public path
In react, fetch is used to request JSON data, and HTML page is returned

Please tell me, what is the cause of this? JSON has been unsuccessful in requesting?

2 Answers
Best Answer
Water stop answered 1 month ago

fetchTo put it bluntly, it’s aajaxRequestajaxMust behttpRun under protocol, put inpublicDirectory, directfetch('/wuhan.json')Try

Too little to eat replied 1 month ago

It turns out that it must be under the HTTP protocol. Before asking questions, I tried to put JSON in node modules and found that it can be requested, but not moved out. I want to ask why other files are not under the HTTP protocol

hyrijk answered 1 month ago

Can notfetchA file, maybe what you need is ajson-server