微信小程序 request接口的封装实例代码
微信小程序request接口的封装实例代码
小程序request接口的封装(本质上是对request回调函数再次回调)
module.exports.getData=function(url){
vardata=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};
varmethod=arguments.length>2&&arguments[2]!==undefined?arguments[2]:'POST';
varheader=arguments.length>3&&arguments[3]!==undefined?arguments[3]:{'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'};
returnnewPromise(function(resolve,reject){
wx.request({
url:url,
data:data,
method:method,
header:{'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'},
success:function(res){
resolve(res)
},
fail:function(res){
reject(res)
}
})
})
}
调用方法
varcommon=require("../../common/common.js")
common.getData(postUrl)
.then(function(data){
this.setData({
//....
});
console.log(data);
});
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短