支付宝小程序my.request在小程序开发者工具上运行正常,但是真机测试出现问题

问题描述:最近开发支付宝小程序,使用网路请求接口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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值