【问题标题】:React JS - How to pass body request in GET of fetch? [duplicate]React JS - 如何在获取的 GET 中传递正文请求? [复制]
【发布时间】:2022-01-11 11:55:47
【问题描述】:

我的 Url 参数太大。所以我试图通过在request.body 中传递它来获取该内容。 API 在测试时可以与 postman 一起正常工作。但无法在 React

中获取它

我正在使用 ExpressJS

API: http://localhost:5000/api/v1

反应代码:


let result = await fetch("http://localhost:8080/autocomplete",{
            method: 'GET',
            body: {
                "text": "something new"
            } 
});

console.log(await result.json())   // Not get my data here

错误:

TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have a body.

我如何解决这个问题?

【问题讨论】:

  • 使用方法:'POST'而不是GET

标签: javascript reactjs get header fetch


【解决方案1】:

这是意料之中的。
从 Mozilla 文档中,它提到:The HTTP GET method requests a representation of the specified resource. Requests using GET should only be used to request data (they shouldn't include data).
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET

【讨论】:

    猜你喜欢
    • 2010-10-24
    • 2018-06-27
    • 1970-01-01
    • 2022-11-14
    • 2018-07-21
    • 2018-04-18
    • 2018-02-19
    • 2016-07-02
    • 2021-03-07
    相关资源
    最近更新 更多