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 API 如何获取响应体? 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 400或500级别的HTTP响应 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01