fetch - JS FetchAPI Get Request 添加原始正文数据
问题描述
我在 Postman 中有一个 GET 请求设置,原始正文数据如下,它可以工作。我可以从服务器获取数据
但是我的 Js fetch api 我不确定如何向它添加请求正文数据。这是我的 GET 请求配置
var requestOptions = {
method: 'GET',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://example.net:/app/v1/cmp/displacer/url", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
但它返回错误
error TypeError: Failed to execute 'fetch' on 'WorkerGlobalScope': Request with GET/HEAD method cannot have body.
服务器只接受对该 URL 的 GET 请求。我试图用谷歌搜索但没有运气。感谢您的时间。
解决方案
fetch 不支持在 GET 请求中发送 BODY(又名有效负载),因为它的行为在 HTTP 中未定义,并且许多其他系统不支持它或以意想不到的方式处理它。如果您需要发送一个 BODY,您可能应该使用与 GET 不同的方法;如果您需要在 GET 请求中发送参数,您可以尝试发送查询字符串。
推荐阅读
- angular - 将按钮添加到具有角度 2 事件的数据表的每一行
- c# - TextChanged 后 TextBox 值为空
- python - Pyspark Cassandra - 为类型为“struct”的 spark 数据框列创建正确的用户定义类型 (UDT)
- python - df[:] 和 df[::] 之间的主要区别是什么?蟒蛇3
- kubernetes - Kubernetes集群如何重启Zuul服务
- bash - 将用户列表添加到光盘使用命令
- python - 数组 tkinter 条目到标签
- c# - 当我从 SSRS 报告中保存 PDF 时,文本在保存的 PDF 中被重叠或截断
- java-native-interface - 从 cocos2d-x 到 Android 调用 JNI 在 Release 模式下不会触发
- c - 不带参数的 pthread_create 函数