我正在尝试在 react-create-app server(localhost:3000) 中使用 fetch 从我的 apache(localhost:80) 获取静态 .json 文件,但它返回我的 react index.html 文件的源!
指定端口号导致“网络错误”
const that=this;
fetch("localhost/myapp/data/structure.json").then((res)=> {return res.text()})
.then(((data)=>{that.setState({structure: data})})).catch((e)=>{alert(e.toString())});
最佳答案
问题完全在于使 react-create-app 与本指南中解释的本地服务器一起工作 https://daveceddia.com/create-react-app-express-backend/
简而言之,我需要在我的 package.json 中放置一个代理属性,其值等于我本地服务器的地址。就我而言:
"proxy": "http://localhost:80"
关于javascript - Fetch 返回我自己的 index.html 的 html 源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44852313/