微信小程序 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);
});
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短