当前位置:网站首页>Simple encapsulation of native fetch requests
Simple encapsulation of native fetch requests
2022-07-18 17:50:00 【Qin ~ ~】
Native fetch Request simple encapsulation
1、 Common request encapsulation
<script>
/*
query: yes get request , Parameters need to be spliced to url After
request: yes post request , utilize body Let's pass on the parameters
*/
export const postData = async (url = '', data: {query?: any, request?: any, methodType?: boolean}) => {
/* about url To deal with */
let newUrl: string = '';
if(data?.query) {
let queryParams: string = '';
for(let key in data?.query) {
queryParams += `${key}=${data?.query[key]}&`;
}
newUrl = url + `?${queryParams}`;
}else {
newUrl = url;
}
const response = await fetch(newUrl, {
method: data?.methodType ? 'GET' : 'POST',
/* Allow cross-domain */
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data?.request),
/* carry cookie */
credentials:'include',
})
return response.json();
}
</script>
2、 Upload file request
<script>
/*
What needs special attention here is : Cannot configure headers, If you add this attribute , Request will make an error
*/
const formData = new FormData();
formData.append('upload',files?.[0])// The first parameter is attribute , The second parameter is the uploaded text stream information set
fetch(newUrl, {
method: "POST",
credentials: 'include',
mode:'cors',
body: formData
})
</script>
边栏推荐
- If an element is above another element, clicking on the above element will trigger the following element event operation
- 任意组件间通信的两种方法
- [paper reading] multi task attention based semi supervised learning for medical image segmentation
- exchange
- 什么是丢包,为什么会丢包
- 网络原理之TCP/IP协议
- 美称未对俄农产品“设障” 俄议员批美国是危机“始作俑者”
- "Wild pointer" and Dachang classic dynamic memory error written test questions
- W806 development board driver ov2640 reads JPEG pictures 1600x1200 resolution
- w806开发板驱动ov2640读取jpeg图片1600x1200分辨率
猜你喜欢

Outdoor LED display has a clever way to deal with hot weather

HCIA-R&S自用笔记(8)IP地址基础、子网掩码、子网划分

Tianyi beyond has a pre-sale of 157700 from the reform, and gross Innovation: the product power is absolutely in the first camp

V831——二维码识别

可爱的小猫咪

QT creator debug mode breakpoint does not work mincw can

【优先队列(堆)】二项队列类模板的实现

Win10重装的方法?一键重装Win10的图文版教程

网络原理之TCP/IP协议

作用域、构造器详解
随机推荐
什么是丢包,为什么会丢包
多叉树--->B树和B+树
网络原理之TCP/IP协议
QT4:从零开始做“工作日记”开发(1)-Worklog项目
Notes on deep learning - regularization, optimizer, linear regression, logistic regression
One week's wonderful content sharing (issue 12) repetition
今日睡眠质量记录85分
【CVA估值训练营】如何快速读懂上市公司年报(第一讲)
V831 - apriltag tag identification
新书上市 | C 语言经典教材配套“习题解答”,原书累计印数 10 万 +
balanced binary tree
Ericsson asked for prohibition on the grounds of infringement, and apple returned to the United States to counterclaim. This scene is so familiar
VIM use learning and ideavim (continuous supplement)
删除某个元素所有子节点的方法
2022年起重机械指挥考试模拟100题模拟考试平台操作
Py之lime:lime库的简介、安装、使用方法之详细攻略
QT creator debug mode breakpoint does not work mincw can
哪些快速提供开发效率的vite插件推荐
PostgreSQL source code (10) xlog assembly
SAP ABAP CDS view 视图的 Replacement 技术介绍