简体   繁体   English

为什么 req.body 为空 {} (GET REQUEST)

[英]Why is req.body empty {} (GET REQUEST)

This is my Frontend code这是我的前端代码

  const fetchData = () => {
    const options = {
        method: 'GET',
        url: 'http://localhost:1337/user/chart',
        headers: {'x-access-token': sessionStorage.getItem('token')},
        body: [chartData.datasets]
      }
      axios.request(options).then((response) => {
        console.log(response)
        }).catch((error) => {
        console.error(error)})
}

This is backend这是后端

app.get('/user/chart', async (req, res) => {
    const token = req.headers['x-access-token']
    if (!token){
        return res.status(404).json({ success: false, msg: "Token not found" });
       }

    try {
        const decoded = jwt.verify(token, process.env.access_secret)
        const email = decoded.email
        await User.updateOne(
            { email: email },
            { $set: {} },

        )
        console.log(req.body)
        return res.status(200).json({message: 'ok', label:[]})
    } catch (error) {
        console.log(error)
        res.json({ status: 'error', error: 'invalid token' })
    }

})

When I console.log(req.body) it is an empty {}.当我使用 console.log(req.body) 时,它是一个空的 {}。 Why is it empty?为什么是空的? I am using a GET request to retrieve the chart data我正在使用 GET 请求来检索图表数据

Axios API does not accept body on get get request you can send parameters with params example Axios API在 get get 请求中不接受 body 您可以使用 params 示例发送参数

const url = '/user/chart';

const config = {
  headers: {'x-access-token': sessionStorage.getItem('token')},
  params:{someKey:chartData.datasets}
};

axios.get(url, config)

Axios doesn't support setting a body for a get request, see the docs or this related question . Axios 不支持为获取请求设置正文,请参阅文档此相关问题

Though, I'd also recommend to reconsider your design.不过,我也建议您重新考虑您的设计。 Typically the body isn't used in a GET request.通常,主体不用于 GET 请求。 If you're sending data to the server, you likely want to use POST or PUT instead.如果您要向服务器发送数据,您可能希望改用 POST 或 PUT。 If you just want to pass a parameter, then you likely want to use request parameters.如果您只想传递一个参数,那么您可能想要使用请求参数。

If you absolutely need to send a body in your GET request, then you'll need to use a different tool.如果您确实需要在 GET 请求中发送正文,那么您将需要使用不同的工具。

frondend //前端//

const fetchData = () => {
const options = {
    method: 'POST',
    url: 'http://localhost:1337/user/chart',
    headers: {'x-access-token': sessionStorage.getItem('token')},
    body: {name : "xx",mail:"xx@"}
  }
  axios.request(options).then((response) => {
    console.log(response)
    }).catch((error) => {
    console.error(error)})
 }

backend //后端//

app.post('/user/chart', async (req, res) => {
const {name , mail} = req.body
const token = req.headers['x-access-token']
if (!token){
    return res.status(404).json({ success: false, msg: "Token not found" });
   }

try {
    const decoded = jwt.verify(token, process.env.access_secret)
    const email = decoded.email
    await User.updateOne(
        { email: email },
        { $set: {} },

    )
    console.log(req.body)
    return res.status(200).json({message: 'ok', label:[]})
} catch (error) {
    console.log(error)
    res.json({ status: 'error', error: 'invalid token' })
}

})Ï })我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM