微信小程序 wx.request方法的异步封装实例详解
wx-promise-request是对微信小程序wx.request方法的异步封装。
解决问题
支持Promise(使用es6-promise库)。
管理请求队列,解决request最大并发数超过10会报错的问题。
下载
npminstallwx-promise-request
然后拷贝dist/index.js文件到你的小程序项目中。
使用
import{request}from'./wx-promise-request';
request({
url:'test.php',
data:{
x:'',
y:'',
},
header:{
'content-type':'application/json',
},
})
.then(res=>console.log(res))
.catch(error=>console.error(error))
API
setConfig(object)
通过setConfig配置wx-promise-request,如:使用qcloud提供的request方法;使用其他Promise库等等。
import{request,setConfig}from'./wx-promise-request';
importqcloudfrom'./vendor/qcloud-weapp-client-sdk/index';
importPromisefrom'bluebird';
setConfig({
request:qcloud.request,
Promise,
})
request({
url:'test.php',
})
.then(res=>console.log(res))
.catch(error=>console.log(error));
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短