小程序用Promise简单封装wx.request的POST请求
const inter = ‘主域名’;
var obj={};
const post=(url,obj)=>{
return new Promise( (resolve,reject)=>{
wx.showLoading({ title: ‘加载中’,mask:true})
wx.request({
url: inter+url,
method:‘post’,
data:obj,
header: {
‘content-type’: ‘application/x-www-form-urlencoded’,
},
success: function (res) {
wx.hideLoading();
if (res.statusCode != 200) {
reject({ error: ‘服务器忙,请稍后重试’, code: 500 });
return;
}
resolve(res.data);
},
fail: function (res) {
reject({ error: ‘网络错误’, code: 0 });
},
complete: function (res) {
wx.hideLoading();
}
})
}).catch(err=>{
reject(err)
console.log(‘请求失败了’,err)
})
}
module.exports=post;