app.js(封装wx.request)
getAjax: function (url, data, callback){
wx.request({
url: 'http://127.0.0.1/api/'+url,
header: {
'content-type': 'application/x-www-form-urlencoded',
},
method: 'POST',
data: data,
dataType: 'json',
success: function (res) {
if(res.data.code == 1){
callback(res.data);
}else{
wx.showToast({
title: res.data.msg,
icon: "none"
})
}
}
})
}
调用封装方法
const app = getApp();
Page({
onLoad: function (options) {
// 第二个参数如果不需要则为{}
app.getAjax('web/lists',{id : id},this.callback);
},
// 成功回调函数
callback: function(res){
var that = this;
console.log(res)
...
}
})