sai*_*men 5 javascript fetch reactjs
我试图在 react-create-app 服务器(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())});
Run Code Online (Sandbox Code Playgroud)
彻头彻尾的问题是使 react-create-app 与本地服务器一起工作,这在本指南中进行了解释https://daveceddia.com/create-react-app-express-backend/
简而言之,我需要在 package.json 中放置一个代理属性,其值等于我本地服务器的地址。就我而言:
"proxy": "http://localhost:80"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4905 次 |
最近记录: |