JS FetchAPI Get 请求添加原始身体数据

问题描述 投票:0回答:1

我在 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 请求。 我尝试用谷歌搜索但没有运气。 谢谢您的宝贵时间。

javascript postman fetch-api
1个回答
2
投票

在 GET 请求中发送 BODY(又名有效负载) fetch 不支持,因为其行为在 HTTP 中未定义,并且许多其他系统不支持它或以意想不到的方式处理它。如果您需要发送 BODY,您可能应该使用与 GET 不同的方法;如果您需要通过 GET 请求发送参数,您可以尝试发送查询字符串

© www.soinside.com 2019 - 2024. All rights reserved.