wepy使用Promise简易封装请求

network/request.js

//封装ajax请求

const http = (url,type,parameter) => {
  return new Promise((resolve,reject) => {
    wx.request({
      url:url,
      method:type,
      success:function (res) {
        resolve(res.data);
      },
      fail:function (err) {
        reject(err)
      }
    })
  })

}

export {http}

在index.wpy中使用

    import { http } from '../network/request';
    http("https://easy-mock.com/mock/5cc66aee7a9a541c744c9c07/example/restful/:id/list","GET").then(function (res) {
      console.log(res)
    }).catch(function (err) {
    
    });

你可能感兴趣的:(wepy使用Promise简易封装请求)