无法在 ReactJS 中使用 Axios 获取 API
Can't Fetch API with Axios in ReactJS
我已经完成并尝试了教程,但结果是一样的
当我想用 axios 检索 API 时,结果同样难以辨认。
也许这可能是一个重复的问题,但我很困惑地完成它。
请帮忙
这是我的代码
constructor(){
super();
this.state={
datas:[],
}
}
componentDidMount() {
axios
.get('/jobs')
.then(res => {
const updatedData = res.datas.map(data => {
return {
...data
};
});
this.setState({ datas: updatedData, err: null });
// console.log(response)
})
.catch(err => {
this.setState({ err: true });
});
}
从渲染端
render(){
let datas = <p style={{textAlign:'center'}}>Something went wrong!</p>
if(!this.state.err){
let datas = this.state.datas.map(data => {
return <Card
key={data.id}
desription={data.desription}
company={data.company}
/>
})
}
return(
<div>
<section>
<Navigation/>
</section>
<section>
<SearchBox></SearchBox>
</section>
<section>
<Category></Category>
</section>
<main>
{datas}
</main>
</div>
)
}
当我调用它时从控制台
here's the image
卡片组件图片:
from card components
实际错误是什么?在页面上。
并且 res.datas
应该是 res.data
因为响应没有 datas
.
我的帐户被一些反对票问题阻止了,有趣的是我必须重新编辑它们,即使我已经接受了 answer.I 不明白这样做有什么意义 this.I 我对这个 Whosebug 系统感到非常沮丧。
现在,我基本上只能不停地编辑我的问题,而且都得到了解答。这太荒谬了!!!
我已经完成并尝试了教程,但结果是一样的
当我想用 axios 检索 API 时,结果同样难以辨认。
也许这可能是一个重复的问题,但我很困惑地完成它。
请帮忙
这是我的代码
constructor(){
super();
this.state={
datas:[],
}
}
componentDidMount() {
axios
.get('/jobs')
.then(res => {
const updatedData = res.datas.map(data => {
return {
...data
};
});
this.setState({ datas: updatedData, err: null });
// console.log(response)
})
.catch(err => {
this.setState({ err: true });
});
}
从渲染端
render(){
let datas = <p style={{textAlign:'center'}}>Something went wrong!</p>
if(!this.state.err){
let datas = this.state.datas.map(data => {
return <Card
key={data.id}
desription={data.desription}
company={data.company}
/>
})
}
return(
<div>
<section>
<Navigation/>
</section>
<section>
<SearchBox></SearchBox>
</section>
<section>
<Category></Category>
</section>
<main>
{datas}
</main>
</div>
)
}
当我调用它时从控制台 here's the image
卡片组件图片: from card components
实际错误是什么?在页面上。
并且 res.datas
应该是 res.data
因为响应没有 datas
.
我的帐户被一些反对票问题阻止了,有趣的是我必须重新编辑它们,即使我已经接受了 answer.I 不明白这样做有什么意义 this.I 我对这个 Whosebug 系统感到非常沮丧。
现在,我基本上只能不停地编辑我的问题,而且都得到了解答。这太荒谬了!!!