app.js
//这里是post请求
apiPost: function (url, data, callback) {
var that = this;
//请求头
var header = {
'content-type': 'application/x-www-form-urlencoded'
};
//请求
wx.request({
url: url,
data: data,
method: 'POST',
dataType: 'json',
header: header,
success: (res) => {
// console.log("res:", res);
return callback(res.data);//成功后回调
},
fail: function (res) {
wx.showModal({
// cancelColor: 'cancelColor',
title: '请联系管理员',
content: res
})
},
complete: function (res) {
console.log("res:", res);
}
})
},
//这里是get请求
apiGet: function (url, data, callback) {
var header = {
'content-type': 'application/json;charset=UTF-8',
"X-Requested-With": 'XMLHttpRequest',
};
wx.request({
url: url,
data: data,
method: 'GET',
dataType: 'json',
header: header,
success: function (res) {
callback(res.data)
},
fail: function (res) {
console.error(url + '请求失败')
},
complete: function (res) {
}
})
},
使用时: app.apiget(参数) app.apipost(参数)