在微信小程序中请求后端接口,我们配合使用微信提供的模态框来显示加载的状态,以及防止用户重复点击,直接上代码
//主要参考 showLoading 和 hideLoading这一块。
function postReq(url, data, cb) {
wx.showLoading({
title: '加载中',
})
console.log("header=="),
console.log(header),
wx.request({
url: rootDocment + url,
header: header,
data: data,
method: 'post',
success: function (res) {
wx.hideLoading();
return typeof cb == "function" && cb(res.data)
},
fail: function () {
wx.hideLoading();
wx.showModal({
title: '网络错误',
content: '网络出错,请刷新重试',
showCancel: false
})
return typeof cb == "function" && cb(false)
}
})
}