【问题标题】:JS FetchAPI Get Request add raw body dataJS FetchAPI Get Request 添加原始正文数据
【发布时间】:2021-04-01 22:22:43
【问题描述】:

我在 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 postman fetch-api


    【解决方案1】:

    在 GET 请求中发送 BODY(也称为有效负载)是 unsupported by fetch,因为 its behavior is undefined in HTTP 和许多其他系统不支持它或以意想不到的方式处理它。如果您需要发送一个 BODY,您可能应该使用与 GET 不同的方法;如果您需要在 GET 请求中发送参数,可以尝试发送query string。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 2017-07-16
      • 2016-07-01
      • 2016-03-11
      相关资源
      最近更新 更多