在新建的Vue项目中运用vue-resource 进行数据请求
login:function(){
let ticket;
this.$http.get('xxx').then(
(response)=>{
ticket = response.data;
console.log(ticket) //
console.log( typeof ticket)
},(response)=>{
console.log(response)
}
)
}
ticket //{"data":"86iwebqy8n","flag":1,"msg":"操作成功"}
typeof ticket //string
Paste_Image.png
然后用对象的办法获取ticket中的data
ticket.data //underfined
typeof ticket //string
结果我懵逼了 用了typeof才知道是string类型
果断用JSON.parse() 转成object
代码如下
login:function(){
let ticket;
this.$http.get('xxx').then(
(response)=>{
ticket = JSON.parse(response.data);
console.log(ticket)
console.log( typeof ticket)
},(response)=>{
console.log(response)
}
)
}
Paste_Image.png
这个获取data才是对的
ticket.data //pmiwec0dsb
绝对是个坎
网友评论