0
点赞
收藏
分享

微信扫一扫

封装微信小程序请求后端api的方法

慎壹 2024-04-03 阅读 20

在微信小程序中请求后端接口,我们配合使用微信提供的模态框来显示加载的状态,以及防止用户重复点击,直接上代码


//主要参考 showLoading   和 hideLoading这一块。
function postReq(url, data, cb) {
  wx.showLoading({
    title: '加载中',
  })
    console.log("header=="),
    console.log(header),
    wx.request({
      url: rootDocment + url,
      header: header,
      data: data,
      method: 'post',
      success: function (res) {
        wx.hideLoading();
        return typeof cb == "function" && cb(res.data)
      },
      fail: function () {
        wx.hideLoading();
        wx.showModal({
          title: '网络错误',
          content: '网络出错,请刷新重试',
          showCancel: false
        })
        return typeof cb == "function" && cb(false)
      }
    })
 
}

举报

相关推荐

0 条评论