在 AXIOS 中发送 GET 方法的请求正文会引发错误

tec*_*tie 4 javascript reactjs

我有一个 React 应用程序,我将 POST 方法更改为 GET 并按原样请求正文。它适用于 POST 请求,但是当我将方法更改为 GET 时,它给了我错误 -

message: "org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public 
Run Code Online (Sandbox Code Playgroud)

我的前端代码-

export const setData = (getData)  => dispatch => {
    axios({
        method: 'GET',
        url: 'http://localhost:8080/api',
        headers: {
          'Content-Type': 'application/json'
        },
        data: getData
      })
      .then (response => {
      dispatch({
        type: API_DATA, 
        payload: response.data
      })
      dispatch({
        type: SET_SEARCH_LOADER, 
        payload: false
      })
      })
      .catch(function(error) {       
      })
}
Run Code Online (Sandbox Code Playgroud)

有人可以让我知道我在这里缺少什么吗?根据我的理解,http 允许有 GET 方法的请求正文。

Que*_*tin 13

根据我的理解,http 允许有 GET 方法的请求正文。

虽然这在技术上是正确的(尽管更准确地说,它只是没有明确禁止它),但这是一件非常奇怪的事情,而且大多数系统并不期望 GET 请求有主体。

因此,很多图书馆都不会处理这个问题。

Axois 的文档说:

  // `data` is the data to be sent as the request body
  // Only applicable for request methods 'PUT', 'POST', and 'PATCH'
Run Code Online (Sandbox Code Playgroud)

在幕后,如果您在 Web 浏览器中运行 Axios 客户端,它将使用 XMLHttpRequest。如果你看一下规范,它说:

client . send([body = null])

发起请求。body 参数提供请求正文(如果有),如果请求方法是 GET或 HEAD,则忽略该正文。