当前位置:网站首页>基于uniapp异步封装接口请求简介
基于uniapp异步封装接口请求简介
2022-04-23 13:04:00 【孤身不觉晚】
uni.request({
url: 'https://www.example.com/request', //仅为示例,并非真实接口地址。
data: {
text: 'uni.request'
},
header: {
'custom-header': 'hello' //自定义请求头信息
},
success: (res) => {
console.log(res.data);
this.text = 'request success';
}
});
这是uniapp官网发起请求的方法,可见其为异步请求;
接下来,我们来二次封装咱们的项目逻辑,如果你对Promise不大了解,建议先去了解了解!
request.globalRequest = (url, method, data) => {
const token = uni.getStorageSync('token');
headers['Content-Type'] = 'application/x-www-form-urlencoded'
if (token != ""){
headers['Authorization'] = 'Bearer' + token
}//请求携带token
return uni.request({
url: url,
method,
data: data,
dataType: 'json',
sslVerify:false,
header: headers
}).then(res => {
// return res[1]
if (res[1].data.err == 0) {
//后端业务逻辑,err=0成功,err=1失败,根据自己业务逻辑改变。
return res[1]
} else {
throw res[1].data //抛出异常
}
}).catch(parmas => {
switch (parmas.code) {
case 401:
// 登录失败,未授权
uni.clearStorageSync()//同步清理本地数据缓存。
break
default:
uni.showToast({
//弹出异常提醒
title: parmas.msg,
icon: 'none'
})
return Promise.reject()
break
}
})
}
export default request
版权声明
本文为[孤身不觉晚]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_40043424/article/details/103629596
边栏推荐
- Metalama简介4.使用Fabric操作项目或命名空间
- 拥抱机器视觉新蓝海,冀为好望开启数字经济发展新“冀”遇
- 31. 下一个排列
- Jupiter notebook installation
- Teach you to quickly develop a werewolf killing wechat applet (with source code)
- 8086 of x86 architecture
- STM32 tracking based on open MV
- leetcode-791. 自定义字符串排序
- GIS practical tips (III) - how to add legend in CASS?
- 98. Error s.e.errormvcautoconfiguration $staticview reported by freemaker framework: cannot render error page for request
猜你喜欢

melt reshape decast 长数据短数据 长短转化 数据清洗 行列转化

How to click an object to play an animation

Idea的src子文件下无法创建servlet

GIS practical tips (III) - how to add legend in CASS?

SSM框架系列——Junit单元测试优化day2-3

22. Bracket generation
![[untitled] make a 0-99 counter, P1 7 connected to key, P2 connected to nixie tube section, common anode nixie tube, P3 0,P3. 1. Connect the nixie tube bit code. Each time you press the key, the nixie](/img/d1/b0ec21419db0cd3641f5fae8458c50.png)
[untitled] make a 0-99 counter, P1 7 connected to key, P2 connected to nixie tube section, common anode nixie tube, P3 0,P3. 1. Connect the nixie tube bit code. Each time you press the key, the nixie

安装nngraph

CVPR 2022&NTIRE 2022|首个用于高光谱图像重建的 Transformer

100 GIS practical application cases (51) - a method for calculating the hourly spatial average of NC files according to the specified range in ArcGIS
随机推荐
There is no need to crack the markdown editing tool typora
mysql支持ip访问
7_Addmodule和基因加和法add 得到的细胞类型打分在空间上空转对比
No idle servers? Import OVF image to quickly experience smartx super fusion community version
Community version Alibaba MQ ordinary message sending subscription demo
SSM框架系列——数据源配置day2-1
100 GIS practical application cases (52) - how to keep the number of rows and columns consistent and aligned when cutting grids with grids in ArcGIS?
Teach you to quickly develop a werewolf killing wechat applet (with source code)
SQL exercise question 1
office2021安装包下载与激活教程
22. Bracket generation
mysql8安装
21 天学习MongoDB笔记
8086 of x86 architecture
V-model binding value in El select, data echo only displays value, not label
22. 括号生成
Customize the shortcut options in El date picker, and dynamically set the disabled date
Recovering data with MySQL binlog
AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters
Van uploader upload picture implementation process, using native input to upload pictures