当前位置:网站首页>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
边栏推荐
- JDBC connection pool
- Install nngraph
- mui picker和下拉刷新冲突问题
- 31. 下一个排列
- filter()遍历Array异常友好
- Translation of attention in natural language processing
- 5 free audio material websites, recommended collection
- 31. Next arrangement
- Design and manufacture of 51 single chip microcomputer solar charging treasure with low voltage alarm (complete code data)
- Kernel error: no rule to make target 'Debian / canonical certs pem‘, needed by ‘certs/x509_ certificate_ list‘
猜你喜欢

Mysql8 installation

Pytorch: a pit about the implementation of gradreverselayer

Important knowledge of transport layer (interview, retest, final)
![[51 single chip microcomputer traffic light simulation]](/img/70/0d78e38c49ce048b179a85312d063f.png)
[51 single chip microcomputer traffic light simulation]

内核错误: No rule to make target ‘debian/canonical-certs.pem‘, needed by ‘certs/x509_certificate_list‘

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)

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?

CGC: contractual graph clustering for community detection and tracking

22. 括号生成

nodeJs + websocket 循环小案例
随机推荐
进程虚拟地址空间区域划分
Calculate the past date and days online, and calculate the number of live days
Date time type in database
Use Proteus to simulate STM32 ultrasonic srf04 ranging! Code+Proteus
Customize classloader and implement hot deployment - use loadclass
SQL exercise question 1
5 free audio material websites, recommended collection
Go language array operation
Start mqbroker CMD failure resolution
Temperature and humidity monitoring + timing alarm system based on 51 single chip microcomputer (C51 source code)
Free and open source agricultural Internet of things cloud platform (version: 3.0.1)
世界读书日:我想推荐这几本书
Golang implements MD5, sha256 and bcrypt encryption
The El table horizontal scroll bar is fixed at the bottom of the visual window
Design of body fat detection system based on 51 single chip microcomputer (51 + OLED + hx711 + US100)
[untitled] PID control TT encoder motor
How to convert opencv pictures to bytes
Process virtual address space partition
4.22学习记录(你一天只做了水题是吗)
安装nngraph