当前位置:网站首页>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
边栏推荐
- 将opencv 图片转换为字节的方式
- 内核错误: No rule to make target ‘debian/canonical-certs.pem‘, needed by ‘certs/x509_certificate_list‘
- Pytorch: a pit about the implementation of gradreverselayer
- MySQL —— 16、索引的数据结构
- (1) Openjuterpyrab comparison scheme
- 安装nngraph
- Three channel ultrasonic ranging system based on 51 single chip microcomputer (timer ranging)
- 1130 - host XXX is not allowed to connect to this MySQL server error in Navicat remote connection database
- Embrace the new blue ocean of machine vision and hope to open a new "Ji" encounter for the development of digital economy
- Design of STM32 multi-channel temperature measurement wireless transmission alarm system (industrial timing temperature measurement / engine room temperature timing detection, etc.)
猜你喜欢

About the 'enum' enumeration type and structure.

31. Next arrangement

Pytorch: a pit about the implementation of gradreverselayer

The quill editor image zooms, multiple rich text boxes are used on one page, and the quill editor upload image address is the server address

8086 of x86 architecture

Use compressorjs to compress pictures, optimize functions, and compress pictures in all formats

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

产品开发都应该知道的8个网站,增强工作体验

Free and open source intelligent charging pile SaaS cloud platform of Internet of things

Use Proteus to simulate STM32 ultrasonic srf04 ranging! Code+Proteus
随机推荐
How to convert opencv pictures to bytes
软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。
office2021安装包下载与激活教程
Jupiter notebook installation
[untitled] PID control TT encoder motor
MySQL supports IP access
leetcode-791. Custom string sorting
mysql支持ip访问
教你快速开发一个 狼人杀微信小程序(附源码)
STM32 tracking based on open MV
Important knowledge of network layer (interview, reexamination, term end)
数据库中的日期时间类型
The El table horizontal scroll bar is fixed at the bottom of the visual window
Go language mapping operation
Three channel ultrasonic ranging system based on 51 single chip microcomputer (timer ranging)
mysql 基本语句查询
Teach you to quickly develop a werewolf killing wechat applet (with source code)
Customize the shortcut options in El date picker, and dynamically set the disabled date
Introducing vant components on demand
CGC: contractual graph clustering for community detection and tracking