Fetch 返回我自己的 index.html 的 html 源

2022-01-20 00:00:00 reactjs fetch javascript

我正在尝试在 react-create-app 服务器(localhost:3000) 中使用 fetch 从我的 apache(localhost:80) 获取静态 .json 文件,但它返回我的 react index.html 文件的源代码!

I am trying to use fetch in an react-create-app server(localhost:3000) to get a static .json file from my apache(localhost:80) but it returns source of my react index.html file!

指定端口号会导致网络错误"

Specifying port number results in "networking error"

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/

Downright the problem comes to making react-create-app work with a local server which is explained in this guide https://daveceddia.com/create-react-app-express-backend/

简而言之,我需要在我的 package.json 中放置一个值等于本地服务器地址的代理属性.就我而言:

In short I needed to put a proxy property with the value equal to address of my local server in my package.json. In my case:

  "proxy": "http://localhost:80"

相关文章