【发布时间】:2021-11-10 14:30:34
【问题描述】:
几周前我刚刚开始学习 React,我尝试制作一个 Nike 网站进行练习。当我使用 fetch 向后端请求数据时
p.s)(带有此错误消息 Uncaught (in promise) SyntaxError: Unexpected token
componentDidMount() {
fetch(`${API}${this.props.location.pathname}`)
.then(res => res.json())
.then(data => {
console.log(data);
this.setState({
productsInfo: data.result,
});
});
console.log('apiCheck', API);
}
我期望的请求 URL 是“http://10.58.6.96:8000/products”。但是,requestURL 是“http://localhost:3000/10.58.6.96:8000/products”,其中包括我的本地地址。当我 console.log API 时,它也显示 10.58.6.96:8000。你能帮我解决这个问题吗?如果您需要我的整个代码,请告诉我!非常感谢您的帮助!
【问题讨论】:
-
你在控制台上得到的
API的值是多少?你想打什么 apiGET? -
你能在提到 API 的地方分享你的代码
-
fetch('//${API}${this.props.location.pathname}')前面加上两个斜杠必须做到这一点 -
@AshishKamble 我的 api 是 10.58.6.96:8000 所以当我将 url 获取到后端时,我会得到我想要的数据!
-
@AshishKamble API 只是 export const API = '10.58.6.96:8000';在 config.js 文件中,我将其导入 main.js!
标签: reactjs