微信小程序-简单封装request

const domain = 'http://127.0.0.1:8080'
let header = {
  'Content-type': 'application/x-www-form-urlencoded;charset=utf-8'
}
function request(url, data) {
  let promise = new Promise((resolve, reject) => {
    wx.showLoading({
      title: '加载中'
    })
    wx.request({
      url: domain + url,
      method: 'POST',
      data: data,
      header: header,
      success: (res => {
        wx.hideLoading();
        if (res.statusCode == 200) {
          resolve(res);
        } else {
          reject(res.data);
        }
      }),
      fail: (res => {
        wx.hideLoading();
        wx.showToast({
          title: '网络出错',
          icon: 'none',
          duration: 1500
        })
        reject('网络出错');
      })
    })
  })
  return promise;
}

module.exports = {
  request: request
}

  

posted @ 2020-06-08 15:48  Judicious  阅读(322)  评论(0编辑  收藏  举报