【问题标题】:Axios delete request is not taking the body dataaxios删除请求没有取body数据
【发布时间】:2019-08-24 14:23:45
【问题描述】:

我是react redux 的新手。这里我有一个delete request

export const deleterequest = (url, jdId) =>
    axios.delete(
        url,
        jdId,
        {
            headers: {
                "Authorization": localStorage.getItem("access_token") !== null ? `Bearer ` + localStorage.getItem("access_token") : null,
                "Content-Type": "application/json"
            }
        }
    ).then(data => {
        if (data.status === HttpStatus.OK) {
            return {
                status: data.status,
                payload: data.data
            };
        }
    }).catch(err => {
        return {
            status: err.response ? err.response.data : 'Network error',
            payload: null
        };

所以,我尝试了这个 apporch。 jdId 是一个字符串数组。所以,但是当我以这种方式使用时,我的请求标头不会显示此数据。

那么,我做错了什么。谁能帮我解决这个问题?

【问题讨论】:

标签: reactjs react-redux axios


【解决方案1】:

删除带有正文的请求需要在数据键下设置

export const deleterequest = (url, jdId) =>
    axios.delete(
        url,
        { data: jdId },
        {
            headers: {
                "Authorization": localStorage.getItem("access_token") !== null ? `Bearer ` + localStorage.getItem("access_token") : null,
                "Content-Type": "application/json"
            }
        }
    ).then(data => {
        if (data.status === HttpStatus.OK) {
            return {
                status: data.status,
                payload: data.data
            };
        }
    }).catch(err => {
        return {
            status: err.response ? err.response.data : 'Network error',
            payload: null
 };

【讨论】:

    【解决方案2】:

    在我的情况下,我必须将一个对象传递给data,否则它根本不起作用。

    错误

    axios.delete(url, {
              data: '123',
            });
    

    正确

    axios.delete(url, {
              data: { data: "123" },
            });
    

    【讨论】:

      【解决方案3】:

      我在 0.20.0 版本中遇到了同样的问题,在升级到 0.21.0 后问题得到解决

      【讨论】:

      • 同样的事情发生在我身上。谢谢你的小费。
      【解决方案4】:

      Axios 支持在删除请求中发送正文。在这种情况下,您必须以以下格式发送:

      axios.delete(
          url,
          {
              data: { jdId }, // or data: jdId, depending on how you handle it in the back end
              headers: {
                  "Authorization": localStorage.getItem("access_token") !== null ? `Bearer ` + localStorage.getItem("access_token") : null,
                  "Content-Type": "application/json"
              }
          }
      )
      .then(data => { 
          // rest of your code 
      })
      .catch(err => { 
          // handle error 
      })
      

      Axios 要求删除请求中只有一个配置对象。 (见这里:https://github.com/axios/axios#axiosdeleteurl-config)配置应该有两个可选的键,即。 数据标题data 只是请求正文。

      希望对你有所帮助。

      【讨论】:

        【解决方案5】:

        这样试试,

        在 react js 中,

        deleteData = (question) => {
            if (question !== "") {
                const url = "http://localhost:3001/questions/delete-question/" + question
        
                const headers = {
                    "Content-Type": "application/json"
                }
        
                axios.delete(url, {data: question}, headers)
                    .then(res => {
                        console.log(res)
                    })
                    .catch(err => {
                        console.log(err)
                    })
            }
        }
        

        在后端,

        router.delete('/delete-question/:question', (req, res) => {
            questions.deleteQuestion(req.params.question)
                .then(response => {
                    res.status(200).send(response);
                })
                .catch(error => {
                    res.status(500).send(error);
                })
        })
        
        
        const deleteQuestion = (question) => {
          return new Promise(function(resolve, reject) { 
            pool.query(`DELETE FROM ${QUESTIONS_TABLE} WHERE question = $1`, [question], (error, results) => {
              if (error) {
                reject(error)
              }
              resolve({
                status: 'success',
                message: 'Question deleted'
              })
            })
          })
        }
        

        确保axios参数格式为axios.delete(url, {data: question}, headers){}括号表示数据很重要

        那么 api url 应该是这样的/delete-question/:question

        像这样从前端访问它"http://localhost:3001/questions/delete-question/" + question

        【讨论】:

          【解决方案6】:
          axios.delete(url, {headers: {}, data: {}})
          

          我认为这会奏效

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-04-26
            • 2020-07-28
            • 1970-01-01
            • 2020-03-08
            • 2018-12-24
            • 2019-11-22
            • 2018-06-29
            • 2018-04-10
            相关资源
            最近更新 更多