gpt4 book ai didi

javascript - 无法在 'fetch' : Request with GET/HEAD method cannot have body 上执行 'Window'

转载 作者:行者123 更新时间:2023-12-02 16:12:11 24 4
gpt4 key购买 nike

通过 Postman 获取我的 API 数据工作正常,但在我的 js 应用程序中运行以下代码时出现错误:Request with GET/HEAD method cannot have body.

var myHeaders = new Headers();
myHeaders.append("Authorization", "Bearer mybearertoken");

var formdata = new FormData();
formdata.append("Id", "56afb645-6084-4129-6469-08d91f4770af");

var requestOptions = {
method: 'GET',
headers: myHeaders,
body: formdata,
redirect: 'follow'
};

fetch("https://myapi.com/api/Konfigurator/GetProjekt", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));

最佳答案

根据 the specification ;

If this’s request method is GET or HEAD, then set body to null.

GET 和 HEAD 请求没有主体,因此所有参数都应该在 URL 中。

至于 Postman 的问题,从技术上讲,只要 http Web 服务器可以读取它,您就可以发送任何带有正文的 HTTP 请求。 Nothing prevents get request of having a body .这是关于 URL 参数可以包含多少查询字符串的限制以及人们需要如何将复杂参数提供到 GET 请求中的持续讨论。

但是,有些人只是倾向于将其作为最佳实践,将其他 http 方法用于 body 和其他随之而来的方法。

关于javascript - 无法在 'fetch' : Request with GET/HEAD method cannot have body 上执行 'Window',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67688159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com