gpt4 book ai didi

reactjs - 如何使用 React js 访问从 axios get 返回的数据以显示在网页上?

转载 作者:行者123 更新时间:2023-12-03 13:28:18 25 4
gpt4 key购买 nike

我在 api 中有 ajax 函数,我正在从我的 React js 组件向该函数发出 axios get 请求。如何访问返回的数据并将其显示在网页上。

最佳答案

取决于您想要做什么,但这只是一个例子。

componentDidMount() {
axios
.get(`endpoint`)
.then(res => this.setState({ posts: res.data }))
.catch(err => console.log(err))
}

如果您使用react-router通过路由器的onEnter api进行ajax调用,这也是一个好方法。

关于reactjs - 如何使用 React js 访问从 axios get 返回的数据以显示在网页上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39680773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com