javascript - 在 get 方法中发送带有参数的对象
问题描述
我有这个接收这个过滤器参数的get方法,它是一个带有from的对象,我想将它们作为url中的参数传递
export async function queryEventTimelineFilteredAPI(filter) {
console.log('componentManager', filter)
const response = await fetch('/api/v1/data/queryEventTimeline', {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
})
return await response.json();
}
解决方案
用于JSON.stringify()
将其转换为 JSON,encodeURIComponent()
添加%
特殊字符的正确编码,然后将其连接到 URL。
不需要Content-type
标头,因为 GET 请求没有内容。
export async function queryEventTimelineFilteredAPI(filter) {
console.log('componentManager', filter)
const response = await fetch('/api/v1/data/queryEventTimeline?filter=' + encodeURIComponent(JSON.stringify(filter)), {
method: 'GET',
})
return await response.json();
}
请注意,URL 长度通常非常有限,因此 JSON 过滤器不应很大。最好将其作为 POST 数据发送。
推荐阅读
- javascript - 比较两个不同数组的值
- javascript - 如何获取用户在手机上使用的浏览器版本?
- python - 拆分包含字符串的列表列表
- elasticsearch - 从 Jira 获取数据到 Elasticsearch
- java - C# 等效于 Javas Guavas Maps.uniqueIndex
- angular - AngularFire - 如何在集合查询中显示 Firestore 键和值
- java - 字段需要一个类型的 bean ... 无法找到
- amazon-web-services - AWS Batch:我们如何知道订购批处理作业正在使用哪个计算环境
- android - 如何自定义 Firebase 应用内消息?
- java - 遇到此问题 *使用 -Xlint 重新编译:未选中以获取详细信息。*