Window.location阻止数据删除

问题描述 投票:-1回答:2

我有一个按钮,当按下时,我想删除该对象。这是我的按钮:

<Button onClick={this.handleDelete.bind(this)}> 
    Delete 
</Button>

这是我的handleDelete

handleDelete() {
    axios
      .delete(
        `http://proiectdepozite.laravel/api/clienti/${this.state.client.id}`
      )
      .then((window.location = "/clienti"))
      .catch(error => console.log(error));
  }

现在,如果我按下按钮,它会将我重定向到我想要的位置,但它不会删除任何内容。我试着评论.then(....),它的工作原理应该如此。哪里出错了?

javascript reactjs axios
2个回答
2
投票

.then方法需要回调函数,但你只需传递一些表达式。

handleDelete() {
    axios
      .delete(
        `http://proiectdepozite.laravel/api/clienti/${this.state.client.id}`
      )
      //try this
      .then(()=>(window.location = "/clienti"))
      .catch(error => console.log(error));
  }
//use async await
async handleDelete() {
   try{
    let result = await axios.delete(
       `http://proiectdepozite.laravel/api/clienti/${this.state.client.id}`
      )

     if(result){
       //success logic 
       window.location = "/clienti"
     }

  }
  catch(e){
    throw e;
  }
}


2
投票
axios.delete('url', { data: payload }).then(
  // Observe the data keyword  Very important
  // payload is the request body

   ()=>(window.location = "/clienti")//you can pass as pipe function


)
© www.soinside.com 2019 - 2024. All rights reserved.