小程序wx.request封装


const baseUrl = 'http://localhost:8080/'
const request = (url, data, method) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseUrl + url,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: method,
      data: data,
      success(res) {
        resolve(res);
      }, fail(error) {
        reject(error)
      }
    })

  })
}

const get = (url, data, method) => {
  return request(url, data, 'GET')
}

const post = (url, data, method) => {
  return request(url, data, 'POST')
}

export const login = (code) => {
  console.log(code);
  get('/api/v1/account/login', { a: 'a', b: 'b' }).then(resp => {
    console.log(resp);
  })
  post('/api/v1/account/login', { a: 'a', b: 'b' }).then(resp => {
    console.log(resp);
  })
}

你可能感兴趣的:(小程序wx.request封装)