欢迎光临,了解微信小程序开发,就上易用通!

Charlotte微信小程序开发(二)--网络请求

发布:2018-04-23 12:13浏览: 来源:网络 作者:cola

微信小程序中request函数的封装

将函数写在app.js中,在其他页面引用函数:

//http方法,写在app.js页面


----------


  http: function (msg) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: msg.url,
        data: msg.data,
        method: msg.method,
        success: function (res) {
          if (res.data.status === 0) resolve(res.data);
          else reject(res.data);
        },
        fail: function (res) {
          console.log(res);
        }
      })
    })
  }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
let app=getApp();//引入app.js 文件


----------


let msg = {
    url: address,
    data: { "data": token },
    method: 'POST'
  }
  app.http(msg).then((data)=>{
     consoel.log(data.data);
  },((reject)=>{

  }))




免责声明:本站所有文章和图片均来自用户分享和网络收集,文章和图片版权归原作者及原出处所有,仅供学习与参考,请勿用于商业用途,如果损害了您的权利,请联系网站客服处理。