微信小程序封装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');

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值