Laravel + Vue - 删除对象,但控制台返回错误



我正在尝试成功删除 vue 中的记录。它删除了记录,但我在控制台中收到以下错误。

405(方法不允许(

我在网络选项卡中收到此错误

"exception": "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException",

这在我的 .vue 文件中。

<button @click="deletePet(pet.id)">Delete</button>

这是我的js文件

methods: {
deletePet(id){
var petid = id;
window.axios.delete('/client/pet/' + id)
.then(response => this.onSubmitSuccess(response))
.catch(error => this.onSubmitError(error));;
console.log('Pet Id' + petid);
}
},

这是我的拉拉维尔路线

Route::delete('/client/pet/{id}', 'ClientController@deletePet')->name('client.pet.delete');

这是我在客户端控制器中的方法

public function deletePet($id){
$pet = Pet::find($id);
$pet->delete();
return back();
}
<html>
<head>
<script>
// rename myToken as you like
window.myToken =  <?php echo json_encode([
'csrfToken' => csrf_token(),
]); ?>
</script>
</head>

添加公共标头:

<script>
window.axios.defaults.headers.common = {
'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN' :  window.myToken 
};
</script>

在你的方法中

methods: {
deletePet(id){
var petid = id;
window.axios.delete('/client/pet/', {params:{'id': petid})
.then(response => this.onSubmitSuccess(response))
.catch(error => this.onSubmitError(error));;
console.log('Pet Id' + petid);
}
},

拉拉维尔路线 :

Route::delete('client/pet/{id}', 'ClientController@deletePet')->name('client.pet.delete');

最新更新