uni-app从后端获取JSON数据,使用时显示undefined
uni-app从后端获取数据,使用时显示undefined错误代码uni.request({url: this.config.webUrl+'login1',dataType: 'text',data: {tel:this.phone1,password:this.password},...
·
uni-app从后端获取数据,使用时显示undefined
错误代码
uni.request({
url: this.config.webUrl+'login1',
dataType: 'text',
data: {
tel:this.phone1,
password:this.password
},
success: (res) => {
console.log('request success---->', res.data);
this.res =res.data ;//------------------------------这步出错
/* this.res =JSON.parse(res.data) ; */
console.log("登录code"+this.res.code);
if(this.res.code==500)
{
return uni.showToast({
title: this.res.msg,
icon: 'none',
duration: duration
});
}else if(this.res.code==0)
{
return uni.showToast({
title: '登录成功',
icon: 'success',
mask: true,
duration: duration
});
}
//this.res = '请求结果 : ' + JSON.stringify(res.data);
},
fail: (err) => {
console.log('request fail', err);
uni.showModal({
content: err.errMsg,
showCancel: false
});
},
complete: () => {
this.loading = false;
}
});
出错结果:
后端取得的JSON数据有时是String 有时是object,把后端取得的数据转换成Object的方法:
this.res =JSON.parse(res.data) ;
uni.request({
url: this.config.webUrl+'login1',
dataType: 'text',
data: {
tel:this.phone1,
password:this.password
},
success: (res) => {
console.log('request success---->', res.data);
this.res =JSON.parse(res.data) ;
console.log("登录code"+this.res.code);
if(this.res.code==500)
{
return uni.showToast({
title: this.res.msg,
icon: 'none',
duration: duration
});
}else if(this.res.code==0)
{
return uni.showToast({
title: '登录成功',
icon: 'success',
mask: true,
duration: duration
});
}
//this.res = '请求结果 : ' + JSON.stringify(res.data);
},
fail: (err) => {
console.log('request fail', err);
uni.showModal({
content: err.errMsg,
showCancel: false
});
},
complete: () => {
this.loading = false;
}
});
例如res.data 数据为:string
{"code":"0",
"data":{"user_address":"",
"user_birth":"",
"user_id":7,
"user_name":"",
"user_password":"737373",
"user_photo":"",
"user_sex":true,
"user_tel":"18888888888"},
"msg":"登录成功"}
this.myres=JSON.parse(res.data) ;
Consold.log("登录code"+this.myres.code);
结果
更多推荐
已为社区贡献1条内容
所有评论(0)