欢迎来到258分享网,纯净的网络源码分享基地!

258资源分享网

全部作品
全部作品
网站源码
微信源码
素材特效
源码插件
视频教程
建站学院
热门搜索: 织梦  农业种植  农业  安全设置  官方
258资源分享 > 建站学院 > 微信开发 > 小程序版结合promise的axios风格ajax请求函数

推荐下载

HTML5响应式自适应网咯设计

2020-05-12   浏览:740

高端HTML5响应式企业通用网

2020-05-06   浏览:521

html5响应式外贸网站英文版

2020-05-08   浏览:510

HTML5自适应律师工作室类网

2020-04-04   浏览:504

HTML5影视传媒文化公司类网

2020-05-12   浏览:501

小程序版结合promise的axios风格ajax请求函数

发布时间:2020-10-06  

建议的小程序版本的axios函数,之所以说简易,因为只是用了常用的请求方法外,然后添加了拦截器而已。
具体如下:

wxhttp

命名为wxhttp

具体的请求用法如axios

wxhttp#request(config)

wxhttp#get(url[,config])

wxhttp#delete(url[,config])

wxhttp#head(url[,config])

wxhttp#options(url[,config])

wxhttp#post(url[,data[,config]])

wxhttp#put(url[,data[,config]])

wxhttp#patch(url[,data[,config]])

拦截方法:

请求拦截

wxhttp.interceptors.request.use(handleRequest(config),handleError(err))

注意:handleRequest需要返回处理后的config

返回拦截

wxhttp.interceptors.response.use(handresponse(res))

注意:handleResponse需要返回处理后的res

例子:

import $http from "../../utils/http" export default { name: 'seckillHome', data() { return { } }, onShow() { // 请求拦截 $http.interceptors.request.use(function (config) { console.log(`请求拦截`, config) // 此处设置的数据将与请求的数据进行合并,如果自动同名则以拦截的为准。 config.data = { address: "北京市东城区" } return config }) $http.post('https://www.baidu.com', { name: 'cdd', age: 23 }).then(res => { console.log(`结果是`, res) }) } }
 

因为使用了promise风格,所以可以使用Promise.all方法来进行并发请求。