当前位置:网站首页>Brief introduction of asynchronous encapsulation interface request based on uniapp
Brief introduction of asynchronous encapsulation interface request based on uniapp
2022-04-23 13:05:00 【It's never too late to be alone】
uni.request({
url: 'https://www.example.com/request', // Just for the sample , Not the real interface address .
data: {
text: 'uni.request'
},
header: {
'custom-header': 'hello' // Custom request header information
},
success: (res) => {
console.log(res.data);
this.text = 'request success';
}
});
This is a uniapp Official website The method of initiating the request , It can be seen that it is an asynchronous request ;
Next , Let's encapsulate our project logic again , If you are right about Promise Not to know , It is recommended to understand first !
request.globalRequest = (url, method, data) => {
const token = uni.getStorageSync('token');
headers['Content-Type'] = 'application/x-www-form-urlencoded'
if (token != ""){
headers['Authorization'] = 'Bearer' + token
}// The request to carry 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) {
// Logical back end business ,err=0 success ,err=1 Failure , Change your business logic .
return res[1]
} else {
throw res[1].data // Throw an exception
}
}).catch(parmas => {
switch (parmas.code) {
case 401:
// Login failed , unauthorized
uni.clearStorageSync()// Synchronously clean up the local data cache .
break
default:
uni.showToast({
// Pop up exception reminder
title: parmas.msg,
icon: 'none'
})
return Promise.reject()
break
}
})
}
export default request
版权声明
本文为[It's never too late to be alone]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231303466081.html
边栏推荐
- CVPR 2022 & ntire 2022 | the first transformer for hyperspectral image reconstruction
- pyqt5 将opencv图片存入内置SQLlite数据库,并查询
- Go iris framework implements multi service Demo: start (listen to port 8084) service 2 through the interface in service 1 (listen to port 8083)
- Record Alibaba cloud server mining program processing
- About the 'enum' enumeration type and structure.
- 8086 of x86 architecture
- Softbank vision fund entered the Web3 security industry and led a new round of investment of US $60 million in certik
- HQL find the maximum value in a range
- MySQL -- 16. Data structure of index
- The project file '' has been renamed or is no longer in the solution, and the source control provider associated with the solution could not be found - two engineering problems
猜你喜欢

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

HQL find the maximum value in a range
![[Blue Bridge Cup] April 17 provincial competition brushing training (the first three questions)](/img/7d/23e2a611bc6a0d0239abdc79e2e8cf.png)
[Blue Bridge Cup] April 17 provincial competition brushing training (the first three questions)

51 single chip microcomputer stepping motor control system based on LabVIEW upper computer (upper computer code + lower computer source code + ad schematic + 51 complete development environment)

mui + hbuilder + h5api模拟弹出支付样式

Design of body fat detection system based on 51 single chip microcomputer (51 + OLED + hx711 + US100)

MySQL supports IP access

100 GIS practical application cases (53) - making three-dimensional image map as the base map of urban spatial pattern analysis

软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。

MySQL -- 16. Data structure of index
随机推荐
Proteus 8.10 installation problem (personal test is stable and does not flash back!)
jmeter操作redis
Huawei cloud MVP email
AUTOSAR from introduction to mastery 100 lectures (86) - 2F of UDS service foundation
31. 下一个排列
Customize the shortcut options in El date picker, and dynamically set the disabled date
Record a website for querying compatibility, string Replaceall() compatibility error
Golang implements a five insurance and one gold calculator with web interface
Subscribe to Alibaba demo send business messages
Jupiter notebook installation
STM32 tracking based on open MV
V-model binding value in El select, data echo only displays value, not label
AUTOSAR from introduction to mastery 100 lectures (81) - FIM of AUTOSAR Foundation
async void 导致程序崩溃
mysql8安装
软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。
[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
51 single chip microcomputer stepping motor control system based on LabVIEW upper computer (upper computer code + lower computer source code + ad schematic + 51 complete development environment)
GIS practical tips (III) - how to add legend in CASS?
21 days learning mongodb notes