2019-08-09 15:10:51 3099浏览
今天千锋扣丁学堂HTML5培训老师给大家分享一篇关于小程序Request的另类用法详解,首先小程序中唯一能发送网络请求接口数据的是wx.request接口,当然这个接口存在诸多的限制,例如:10个并发请求限制,https限制(当然在开发阶段是可以关闭此限制),除了wx.request还有其他方法可以实现类型的功能吗?当然是有的,这个思路也源于我之前看到的一篇文章。
// 云函数入口文件
const cloud = require('wx-server-sdk')
const axios = require('axios')
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
const { method, url, data } = event;
const res = await axios.request({
method: method,
url: url,
data: data
});
return { code: 1, data: res.data } || {code: -1, msg: 'error', data: null}
}
async http(options = {}) {
return wx.cloud.callFunction({
name: 'http',
data: {
method: options.method || 'GET',
url: options.url || '',
data: options.data || {}
}
}).then(res => {
return res.result
})
},
async onLoad() {
this.http({
method: 'GET',
url: 'https://www.baidu.com'
}).then(res => {
console.log(res)
})
},
【关注微信公众号获取更多学习资料】 【扫码进入HTML5前端开发VIP免费公开课】