axios 相关

后台接口返回示例:

{
   "errcode": 0,
   "errmsg": "OK"
   "res": {
     "num": "100000000000000000"
   }
}

前端使用 axios 发送请求:

axios.get(url.getData)
.then(res => {
    res.data.res.num
})
.catch(err => console.log(err))

请问:

  1. 根据后台返回示例,要拿到 num,res.data.res.num 这样对吗?
  2. 返回示例里面的 errcode 与 res.status 应该不一样把?
  3. 请求成功之后走 .then( )里面,得到 res 之后是不是还需要根据 errcode 判断一下?

例如:

axios.get(url.getData)
.then(res => {
    if (res.data.errcode === 0) {
        console.log(res.data.res.num)
    } else {
        console.log(res.data.errmsg)
    }
})
.catch(err => console.log(err))


阅读 1.5k
1 个回答

打印下res看看是什么,然后看怎么取吧
errcode 可以表示很多状态,现在0表示成功,以后可能还有1,2
所以最好判断下

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题