当前位置:网站首页>fetch请求-简单记录
fetch请求-简单记录
2022-07-17 00:09:00 【祥仔先生】
主要是记录一下 fetch的post和get请求的发送方法。
方法:GET
const { URLSearchParams } = require('url');
const testUrl = `localhost:3000`;
let userInfo = await fetch(`${testUrl}?${new URLSearchParams(ctx.query)}`, {
headers: {
'Content-Type': 'application/json',
'Cookie': `_ut=${ut}`
},
method: "get"
});
let result = await userRoleInfo.json();说明:
这里使用URLSearchParams来处理get请求的param。
If using node, you can add the fetch API through a package like node-fetch. URLSearchParams comes with node, and can be found as a global object since version 10. In older version you can find it at require('url').URLSearchParams
fetch('https://example.com?' + new URLSearchParams({
foo: 'value',
bar: 2,
}))
当然对于处理param参数 还有其他方法,我还没有实验 就把例子放在这里了
import { stringify } from 'query-string';
fetch(`https://example.org?${stringify(params)}`)方法: POST
post的方法就相对简单了
fetch(url, {
method: 'POST',
body: JSON.stringify(json),
headers: {
'Content-Type': 'application/json'
}
}).then(res => res.json()) // here is my code waites the responce from the server
.then((res) => {
toastr.success('Created Type is sent successfully');
})
.catch(err => {
console.log('Type send failed', err);
toastr.warning('Type send failed');
})边栏推荐
- What are the NFT digital collection platforms? Which platforms are worth collecting?
- Today, the code farmer girl made notes about the life cycle and practiced the dynamic clock
- Punch in 10 interview questions every day - JVM article
- NFT differentiation trend has shown, how to capture value?
- What is "digital collection"?
- [ahu2021 school competition] EZ injection
- Cento7安装mysql5.5以及升级5.7
- Redis 高可用原理
- 文章不会一直保持VIP收费 过一段时间 会开放
- ChunJun支持异构数据源DDL转换与自动执行 丨DTMO 02期回顾(内含课程回放+课件)
猜你喜欢

The popularity of NFT IP licensing is rising, and the era of nft2.0 is coming?

What are the NFT digital collection platforms? Which platforms are worth collecting?

何为“数字藏品”?

nmap和nikto扫描

Introduction to software vulnerability analysis (III)

紅日安全靶場3

Why do you spend 1.16 million to buy an NFT avatar in the library of NFT digital collections? The answer may be found by reviewing the "rise history" of NFT avatars

Libtomcrypt密码库的使用

开源项目丨 Taier 1.1 版本正式发布,新增功能一览为快

软件漏洞分析入门(四)
随机推荐
What is "digital collection"?
基于机器学习技术的无线小区负载均衡自优化
Libtomcrypt密码库的使用
[AHU2021校内赛] ez-injection
情况说明和感想
紅日安全靶場3
基于开源流批一体数据同步引擎ChunJun数据还原—DDL解析模块的实战分享
10 完成一半题目
波卡生态中“中继链”、“DOT”的常见问题解答
binary search
V-model principle and modifier
文章不会一直保持VIP收费 过一段时间 会开放
Cento7安装mysql5.5以及升级5.7
CheckPoint and DataNode
如何建设实时开发平台,深入释放企业实时数据价值?
Common asynchronous sending code writing
Determine whether the two timestamps are the same day
金融学 杂项记录
Introduction to software vulnerability analysis (5)
温州大学X袋鼠云:高等人才教育建设,如何做到“心中有数”