Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
160 views
in Technique[技术] by (71.8m points)

react中用fetch请求json数据,返回的是html页面

问题

在react代码中需要渲染一个地图组件,请求了一个json数据,预期是请求成功后将json数据注入到地图组建中去,但是在控制台中看到的是 返回的是 index.html里面的内容,并没有返回json数据。

我试了下请求别的json文件或者是一个不存在的json文件,返回的也是index.html里面的内容

相关代码

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

控制台报错

clipboard.png

我的项目目录结构

clipboard.png

控制台中查看请求的json,返回的是 public路径中的 index.html里面的内容

clipboard.png


请教下各位,这个是什么原因造成的,json一直请求不成功?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...