http(mathods,url, params) {
var header = {
‘content-type‘: ‘application/json‘,
‘token‘ : wx.getStorageSync(‘token‘) || ‘‘,
} //设置请求头,可以带其他后台验证参数,根据业务逻辑
return new Promise(
(resolve,reject) => {
wx.showLoading({
title: "正在加载中...",
})
wx.request({
url: ‘‘, //请求地址
method: mathods, //请求方法
header: header,
data: params || {}, //请求参数
success: res => {
wx.hideLoading();
resolve(res);
//成功执行方法,参数值为res.data,直接将返回的数据传入
},
fail: function() {
//请求失败
wx.hideLoading();
wx.showToast({
title: ‘服务器错误,请稍后再试!‘,
icon : ‘none‘
})
reject(err)
},
})
}
)
}
const app = getApp();
app.http(‘GET/POST/DELETE/...‘,‘接口地址‘,data).then((res)=>{
//接收到后台返回的res
})
原文:https://www.cnblogs.com/yanghao-blogs/p/14228304.html