Fetch returns html source of the my own index.html(Fetch 返回我自己的 index.html 的 html 源)
问题描述
我正在尝试在 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"
这篇关于Fetch 返回我自己的 index.html 的 html 源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Fetch 返回我自己的 index.html 的 html 源
基础教程推荐
- fetch 是否支持原生多文件上传? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
