爱可信得到API数据 - React.js

问题描述 投票:1回答:1

我使用Axios公司从一个API获取数据,我试图做一些很简单的,我已经这么做过。我可以看到我的请求在控制台上,但我不能输出的数据或执行console.log()消息。

componentDidMount() {
    axios.get("https://dog-api.kinduff.com/api/facts")
    .then( response => {
        console.log("Facts: ")
        this.setState({DogFact:response.data})
    })
    .catch( err => {
        this.setState({error:err.data.message})
    })
}

从API响应是与阵列的对象。

{facts["fact written here"]}

它应该是很简单的,但如果我尝试:

axios.get("https://dog-api.kinduff.com/api/facts")
.then( response => {
    console.log("Facts: ", response) //This wont show up on the console
    this.setState({DogFact:response.facts[0]}) //This wont work.
})

我真的不明白是什么可能是错误的。可能有人也许帮我吗?

reactjs axios
1个回答
1
投票

加入的package.json这一行

"proxy": "https://dog-api.kinduff.com/api/"

那么在你的爱可信调用它改成这样:

axios.get("/facts")
.then( response => {
    console.log("Facts: ", response) 
    this.setState({DogFact:response.facts[0]})
});
© www.soinside.com 2019 - 2024. All rights reserved.