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);

结果
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐