当前位置:   article > 正文

微信小程序封装Ajax请求_微信小程序如何实现封装ajax

微信小程序如何实现封装ajax

var app = getApp();

const serverIp = app.globalData.serverIp;// app.globalData.serverIp="https:app.qcwy.com/website/page"

//使用时需要在app.js的globalData内预先将需要请求的路径保存在serverIp中,使用字符串拼接以向不同的页面发起请求

//例如请求https:app.qcwy.com/website/page+/back/back.aspx       

const Ajax = function (url, mydata, that, fun, failfun) {

wx.request({

  url: serverIp + url,

  data: mydata,

  method: "POST",

  header: {

     'content-type': 'application/x-www-form-urlencoded',

'cookie':"usermessage=username="+escape(wx.getStorageSync("username"))+"&password="+   wx.getStorageSync("password")

},//token验证

success: function (res) {

   if (fun)

   fun(res, that);

},

fail: function(){

   if (failfun)

   failfun(that);

}

})

}

module.exports = {

Ajax: Ajax

}

 

//将以上方法保存在util.js中,在其他页面中引用方法如下:

const util=require('../util.js');

util.Ajax('/back/back.aspx',{'action':'myfun','username':'xiaoxu',t:Math.random()},this,function(data,that){

   if(data.data)

   {

   console.log('I get data');

   that.setData({data:data.data});

   }

},funtion(that){

console.log('fail');

});

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Guff_9hys/article/detail/852201
推荐阅读
相关标签
  

闽ICP备14008679号