JSON读取问题
发布于 5 年前 作者 fang01 3780 次浏览 来自 问答
console.log(res.data);
 
console.log(res.data.message);
 
console.log(res.data.code);

分别打印3条数据,结果如下:

{"code": 200, "message": "success"}
undefined
undefined

突然这个json就读不到数据了,求大神帮忙看看这是啥情况

5 回复

我也是用这个,可以转

字符串不是JSON对象,没法通过key读value。

把字符串parse成JSON对象就行了。

像是异步的问题,打印放在接口请求成功的方法里试试

res.data  这个是字符串吧

得转json

let obj = JSON.parse(res.data);

console.log(obj.message);

回到顶部