赞
踩
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');
});
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。