Axios删除请求未获取正文数据

eanckbw9  于 2023-01-25  发布在  iOS
关注(0)|答案(8)|浏览(197)

我是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
        };

所以,我尝试了这个方法。jdId是一个字符串数组。所以,但是当我用这种方法使用时,我的请求头没有显示这些数据。
那么,我到底做错了什么。有人能帮我吗?

epfja78i

epfja78i1#

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

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
 };
wb1gzix0

wb1gzix02#

在我的例子中,我必须向data传递一个对象,否则它根本无法工作。

错误

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

正确

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

jk9hmnmh3#

我有相同的问题与版本0.20.0和后升级到0.21.0问题得到解决

yvfmudvl

yvfmudvl4#

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 * 和 * header data * 只是请求正文。
希望这对你有帮助。

7gyucuyw

7gyucuyw5#

像这样试试,

    • 在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){}数据的括号很重要
那么APIURL应该类似于/delete-question/:question
"http://localhost:3001/questions/delete-question/" + question这样从前端访问它

mrphzbgm

mrphzbgm6#

axios.delete(url, {headers: {}, data: {}})

我想这会有用的

wkyowqbh

wkyowqbh7#

我使用的是Axios v0。我无法让他们的删除方法工作,即使在编写配置对象时也是如此。
唯一对我有效的方法(2022年)是使用普通的axios对象并定义方法,如下所示:

await axios({
  method: 'DELETE',
  url: 'yoururlhere.com',
  data: {foo: 'bar'},
  headers: {h1: val, ...}
})
yhived7q

yhived7q8#

You can Use Patch To delete 
#frontend 
export const DeleteAdminHallByID = async (id,public_id) =>{
  
    return await axios.patch(`http://localhost:5000/api/v1/hall/hall/${id}`,{public_id},config);
  }; 

#Route folder In nodejs
router.route('/hall/:id').patch(auth,authAdmin,deleteHall)

const deleteHall = async (req, res) => {
  try {
    const { public_id } = req.body;
    const { id } = req.params;
   
    const hall = await Hall.findByIdAndDelete(id);
    if (!hall)
      return res.status(500).json({ msg: "There Is No hall With This ID" });
    console.log("done 2");
    res.status(200).json({ msg: "Hall Deleted.." });
  } catch (error) {
    return res.status(500).json({ msg: error.message });
  }
};

相关问题