【问题标题】:Send body in method GET axios [duplicate]在方法GET axios中发送正文[重复]
【发布时间】:2019-08-29 15:22:04
【问题描述】:

我如何在方法 GET 中发送正文,但我没有收到。

我尝试了,但不工作

const res = await axios({
    method: 'get',
    url: `${this.state.url}Docto/Imagens`,
    headers: {
        Authorization: `Bearer ${Token}`,
        "Content-Type": "application/json"
    },
    processData: false,
    data: { Id: 4075 },
    body: { Id: 4075 }
})

在 Postman 中,请求正常工作

我知道正确的方法是在get时通过查询字符串传递参数,但后端是这样做的。

【问题讨论】:

标签: javascript


【解决方案1】:

GET 请求没有正文,请改用查询字符串。 对于 axios,我通常使用 npm package qs,它也在 axios docs 中使用:

配置:

    {
       ...
       // `paramsSerializer` is an optional function in charge of serializing `params`
      // (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
      paramsSerializer: function (params) {
        return Qs.stringify(params, {arrayFormat: 'brackets'})
      },
      ....
    }

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 2019-05-29
    • 2021-11-15
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多