使用Axios调用API获取JSON数据时,destination返回undefined问题排查
问题原因及解决方法
问题出在数据层级的嵌套上,你忽略了API返回结构里的双层data嵌套:
从你提供的API返回JSON可以看到,Axios的response.data对应的是最外层的data对象,而你要找的destination字段在这个外层对象内部的另一个data子对象里。
修正后的代码
axios.request(options).then(function (response) { // 补充一层 .data 访问内部业务数据对象 console.log(response.data.data.destination); }).catch(function (error) { console.error(error); });
结构拆解
API返回的核心结构(简化后):
{ "data": { "data": { "destination": "INDB", // 其他业务字段... }, "message": "Success", "status": true, "timestamp": 1661021331914 } }
response.data→ 对应最外层的容器对象,包含data、message、status等元信息字段response.data.data→ 对应实际的业务数据对象,里面才包含destination、source等你需要的具体字段
你之前的代码少了一层.data,自然找不到目标字段,返回undefined。
内容的提问来源于stack exchange,提问作者vatsal mandloi
相关产品推荐
相关产品推荐

