微信小程序-【请求数据】

普通请求 wx.request

wx.request({
      url: '',
      data:  {},
      method: 'GET',
      success: function (res) {
      },
      fail: function (res) {
      }
    })

整体封装

var server = function(url, type, params) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'http://localhost:7001/' + url,
      data: params || {},
      method: type || 'GET',
      success: function(res) {
        resolve(res.data)
      },
      fail: function(res) {
        reject(res)
      }
    })
  })
} 

封装添加get方法

server.get = function(url, params) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'http://localhost:7001/' + url,
      data: params || {},
      method: 'GET',
      success: function (res) {
        resolve(res.data)
      },
      fail: function (res) {
        reject(res)
      }
    })
  })
}

封装添加post方法

server.post= function(url, params) {
  return new Promise((resolve, reject) => {
    wx.request({
      url: 'http://localhost:7001/' + url,
      data: params || {},
      method: 'post',
      success: function (res) {
        resolve(res.data)
      },
      fail: function (res) {
        reject(res)
      }
    })
  })
}

你可能感兴趣的:(小程序)