简体   繁体   English

axios拦截器中的变更请求方法

[英]Change request method in axios interceptor

I'm trying to change an axios GET request into a POST request using an interceptor. 我正在尝试使用拦截器将axios GET请求更改为POST请求。 The method seems to be changed, but my params are still affixed to the URL, instead of being sent in the POST body. 该方法似乎已更改,但我的参数仍然附加到URL,而不是在POST正文中发送。

    axios.get(payload.url, {
      params: payload.params || {}
    })

    axios.interceptors.request.use(
      function (config) {
        // check request method -> use post if many params
        if (MY_CONDITION) {
          if (config.method === 'get') {
            console.log('changed to post')
            config.method = 'post'
          }
        }
        return config
      }
    )

Am I missing something? 我错过了什么吗?

Thanks to CD..'s comment, I found the solution. 感谢CD ..的评论,我找到了解决方案。 Params are always attached to the request-URL, while I'd need to use data , since that's what ends up in the POST body . Params始终附加到请求URL,而我需要使用data ,因为这是POST body Posting in case other's need it: 发布以防其他人需要它:

  config.method = 'post'
  config.data = config.params
  config.params = {}

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

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