当前位置:网站首页>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
边栏推荐
猜你喜欢
进程虚拟地址空间区域划分
The accuracy and speed are perfectly balanced, and the latest image segmentation SOTA model is released!!!
初鉴canvas,展示个小小的小案例
Calculate the past date and days online, and calculate the number of live days
[51 single chip microcomputer traffic light simulation]
Design and manufacture of 51 single chip microcomputer solar charging treasure with low voltage alarm (complete code data)
Record the problems encountered in using v-print
Recovering data with MySQL binlog
世界读书日:我想推荐这几本书
AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters
随机推荐
100 GIS practical application cases (53) - making three-dimensional image map as the base map of urban spatial pattern analysis
Synchronously update the newly added and edited data to the list
1130 - host XXX is not allowed to connect to this MySQL server error in Navicat remote connection database
Wu Enda's programming assignment - logistic regression with a neural network mindset
内核错误: No rule to make target ‘debian/canonical-certs.pem‘, needed by ‘certs/x509_certificate_list‘
软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。
mui 关闭其他页面,只保留首页面
pyqt5 将opencv图片存入内置SQLlite数据库,并查询
「玩转Lighthouse」轻量应用服务器自建DNS解析服务器
There is no need to crack the markdown editing tool typora
8086 of x86 architecture
mui 微信支付 排坑
office2021安装包下载与激活教程
Learning materials
filter()遍历Array异常友好
decast id.var measure.var数据拆分与合并
Common problems of unity (1)
100 GIS practical application cases (51) - a method for calculating the hourly spatial average of NC files according to the specified range in ArcGIS
Translation of multi modal visual tracking: review and empirical comparison
(personal) sorting out system vulnerabilities after recent project development