我有一个按钮,当按下时,我想删除该对象。这是我的按钮:
<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(....)
,它的工作原理应该如此。哪里出错了?
.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;
}
}
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
)