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

标签 javascript reactjs fetch-api

我正在尝试在 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/

相关文章:

javascript - 具有模运算的 angularJS 轮播负循环

javascript - 将 localStorage 中的内容附加到 div 脚本后不再起作用

javascript - 使用 fetch API 从数组获取数据时出现问题

go - Axios 和 fetch 在 Golang 中给出空映射,即使在 url 编码时(添加了 header )

javascript - 在 javascript 中使用 fetch 时访问 header

javascript - 遍历文件夹

java - 添加对 SuggestBox 的关注?

React js 组件中的 javascript addEventListener 不起作用

javascript - Reactjs/Graphql : TypeError: Object(. ..) 不是函数

arrays - React 测试库 - 在 fireEvent 之后使用 'await wait()'