javascript - Using fetch to request json data in react returns an html page
PHPz
PHPz 2017-05-19 10:47:17
0
2
1092

question

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 component after the request is successful, but what is seen in the console is that the content in index.html is returned. content, and no json data is returned.

I tried requesting other json files or a non-existent json file, and the content in index.html was also returned

Related codes

 componentWillMount() {
   fetch('../data/wuhan.json')
   .then(function(response) {
     return response.json();
   })
   .then(function(json) {
     echarts.registerMap('wuhan', json);//注册地图组件
   })
   .then(function() {
     this.updataSeries();//处理series
   })
 };

Console error

My project directory structure

Check the requested json in the console and return the content in index.html in the public path


I would like to ask everyone, what is the reason for this? The json request has been unsuccessful?

PHPz
PHPz

学习是最好的投资!

reply all(2)
滿天的星座

fetch说白了就是个ajax请求,ajax必须在http协议下运行,放进public目录下,直接fetch('/wuhan.json')Try it

滿天的星座

Can’t fetch A file, maybe what you need is a json-server

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!