Vuejs2 Axios.delete()419未知状态
我有一个产品列表,但当我试图删除其中一个产品时,出现以下错误 删除419(未知状态) 在postman中,我得到“CSRF令牌不匹配”。这是我的删除函数Vuejs2 Axios.delete()419未知状态,vuejs2,vuex,laravel-8,Vuejs2,Vuex,Laravel 8,我有一个产品列表,但当我试图删除其中一个产品时,出现以下错误 删除419(未知状态) 在postman中,我得到“CSRF令牌不匹配”。这是我的删除函数 public function delete(Request $request) { $product = Product::find($request->id); $product->delete(); return response()->json('Product deleted!'); }
public function delete(Request $request)
{
$product = Product::find($request->id);
$product->delete();
return response()->json('Product deleted!');
}
Vue方法
removeProduct(id)
{
this.$store.dispatch('removeFromProducts', id)
}
Vuex操作
removeFromProducts({commit}, id){
axios.delete(`/api/deleteProduct/${id}`).then((res)=>{
commit('removeFromProducts', res.id)
console.log(res);
}).catch((err)=>{
console.log(err);
})
},
removeFromProducts(state, id){
let i = this.products.findIndex(data => data.id === id);
state.products.splice(i, 1);
},
Vuex突变
removeFromProducts({commit}, id){
axios.delete(`/api/deleteProduct/${id}`).then((res)=>{
commit('removeFromProducts', res.id)
console.log(res);
}).catch((err)=>{
console.log(err);
})
},
removeFromProducts(state, id){
let i = this.products.findIndex(data => data.id === id);
state.products.splice(i, 1);
},
顺便说一句,我要说的是:我认为对于delete,您不需要使用
Vuex
。另外,您不需要array.splice()
方法。签入控制器id即将到来或未与邮递员一起使用dd()。在Vue中,您需要使用id参数调用delete api。我在没有vuex的情况下尝试了它,我尝试了很多方法,但它一直给我419状态和csrf令牌不匹配。我认为它没有将id传递给控制器。您在控制器中获得的id正确吗?我发现了,我的错误是我在api文件夹中编写了route::post而不是route::delete。