小程序api请求400

旧款安卓手机,在请求url带有中文时,会出现请求400的问题

//用encodeURI 转一下中文部分
  let url = Url + "?reason=" + encodeURI(data.reason)

wx.request请求老是报错

仔细检查自己的代码,没有问题呀!在看看后台,也没有问题啊,为什么就会保存呢?接下来就是我亲自的爬坑经验。
问题出在哪了呢?编码格式!!!
将wx.request请求中的
'Content-Type': 'application/json;
//改为 
'Content-Type': 'application/json;charset=UTF-8;' 
//如果还不行再改: 
'Content-Type': 'application/x-www-form-urlencoded'

代码如下:

// HTTP请求
const http = (uri, method, params) => {
  // let url = CTX + uri;
  let url = CTX + encodeURI(uri);
  console.log(uri,encodeURI(uri));
  params = params ? params : {};
  return new Promise(function(resolve, reject) {
    wx.request({
      url: url,
      method: method,
      data: params,
      header: {
        // 'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
        // 'Content-Type': 'application/json;charset=UTF-8',
        // 'Content-Type': 'application/x-www-form-urlencoded',
        // 'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Type': 'json',
        'uuid': wx.getStorageSync('uuid'),
      },
      success(res) {
        if (res.data.status === 0) {
          resolve(res.data);
        } else if (res.data.status === -1) {
          processError(res.data);
          reject(res.data);
        } else {
          processError(res.data);
          reject(res.data);
        }
      },
      fail(res) {
        wx.showToast({
          title: '网络错误',
          icon: 'none',
          duration: 2000
        })
      }
    })
  })
}

// HTTP/GET请求
const httpGet = (uri, params) => {
  return http(uri, 'GET', params);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值