Axios变得不确定

编程入门 行业动态 更新时间:2024-10-25 11:36:09
本文介绍了Axios变得不确定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有React组件FetchQoute,该组件应该从提供的URL中获取响应,但它显示的是 undefined ?

I have React component FetchQoute which is supposed to get response from the provided URL, but it shows undefined instead ?

class FetchQuote extends Component { constructor(props) { super(props); this.state = { jokes: [] }; } componentDidMount() { console.log(res.value); //undefined axios.get('api.icndb/jokes/random') .then((res) => { this.setState({data:res.value.joke});//error TypeError: Cannot read property 'joke' of undefined }) } }

可以轻松地在浏览器中测试该URL,以正确返回JSON数据,包括res.value.joke

That URL can be easily tested in browser to return JSON data correctly, including res.value.joke

推荐答案

浏览器中的JSON响应是什么样的?axios在data属性下返回响应数据.

What's the JSON response look like in the browser? axios returns response data under the data attribute.

例如,如果您的JSON响应如下:

For example if your JSON response looks like:

{ "value": { "joke": "lol"} }

然后您将以类似的方式访问它

Then you would access it like

axios.get('api.icndb/jokes/random') .then((response) => { console.log(response.data.value.joke); })

更多推荐

Axios变得不确定

本文发布于:2023-11-08 06:54:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不确定   Axios

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!