当前位置:网站首页>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
边栏推荐
- Golang implements a five insurance and one gold calculator with web interface
- Start mqbroker CMD failure resolution
- mui picker和下拉刷新冲突问题
- Golang realizes regular matching: the password contains at least one digit, letter and special character, and the length is 8-16
- 22. 括号生成
- AUTOSAR from introduction to mastery 100 lectures (50) - AUTOSAR memory management series - ECU abstraction layer and MCAL layer
- hbuilderx + uniapp 打包ipa提交App store踩坑记
- JMeter operation redis
- Go iris framework implements multi service Demo: start (listen to port 8084) service 2 through the interface in service 1 (listen to port 8083)
- office2021安装包下载与激活教程
猜你喜欢

22. 括号生成

World Book Day: I'd like to recommend these books

Design of STM32 multi-channel temperature measurement wireless transmission alarm system (industrial timing temperature measurement / engine room temperature timing detection, etc.)

MySQL supports IP access

Huawei cloud MVP email

nodejs + mysql 实现简单注册功能(小demo)

Free and open source charging pile Internet of things cloud platform
![Packet capturing and sorting -- TCP protocol [8]](/img/ae/4957a997af725a1bf3f61cb24fc717.png)
Packet capturing and sorting -- TCP protocol [8]

mysql8安装

Jupiter notebook installation
随机推荐
World Book Day: I'd like to recommend these books
内核错误: No rule to make target ‘debian/canonical-certs.pem‘, needed by ‘certs/x509_certificate_list‘
21 days learning mongodb notes
100 GIS practical application cases (51) - a method for calculating the hourly spatial average of NC files according to the specified range in ArcGIS
melt reshape decast 长数据短数据 长短转化 数据清洗 行列转化
STD:: shared of smart pointer_ ptr、std::unique_ ptr
HQL statement tuning
Async void caused the program to crash
将新增和编辑的数据同步更新到列表
100 GIS practical application cases (34) - splicing 2020globeland30
8086 of x86 architecture
async void 導致程序崩潰
Golang realizes regular matching: the password contains at least one digit, letter and special character, and the length is 8-16
Golang implements MD5, sha256 and bcrypt encryption
Wu Enda's programming assignment - logistic regression with a neural network mindset
Navicat远程连接数据库 出现 1130- Host xxx is not allowed to connect to this MySQL server错误
数据库中的日期时间类型
Use Proteus to simulate STM32 ultrasonic srf04 ranging! Code+Proteus
Synchronously update the newly added and edited data to the list
Golang implements a five insurance and one gold calculator with web interface