问题描述:最近开发支付宝小程序,使用网路请求接口my.request时,出现在小程序开发者工具上运行正常,但是一到真机测试中就有问题,得到的返回结果出现在fail的回调函数中,使用JSON.stringify(res)打印出日志显示报错为:"error": 14, "errorMessage": "JSON parse data error"。
问题分析:服务端返回的数据与小程序请求的Datatype类型不一致 datatype有json text base64,默认为json
解决办法:
var that = this;
my.request({
url: `${baseUrl}/alipay/agreementSign`,
headers: {'content-type': 'application/x-www-form-urlencoded'},
method: 'POST',
data: {
userId: alipayUserId
},
dataType: 'text',
success: function(res) {
if (res.data == null){
console.log("该用户已签约,无需签约");
that.setData({
hasSign: true,
});
}else{
console.log("该用户未签约,并获取signStr成功");
my.setStorageSync({
key: 'signStr',
data: res.data,
});
}
},
fail: function(res){
console.log('[request fail]');
},
complete: function(res){
console.log('[request complete]');
}
});
},
dataType改成 text