gpt4 book ai didi

Laravel - 删除 Ajax 请求

转载 作者:行者123 更新时间:2023-12-01 12:35:56 26 4
gpt4 key购买 nike

我正在使用 Laravel 5.0。我创建了一个 RESTful Controller 。现在我想通过ajax调用使用destroy函数。
这是我的JS:

$.ajax({
type: 'POST',
url: '/pv/' + data.id,
data: {_method: 'delete' },
success: function(data){
console.log(data);
}
});

这是我的销毁功能:
public function destroy($id)
{
$pv = PV::find($id);

$pv->delete();

return true;
}

我得到的只是一个 500 错误。

最佳答案

首先检查你在laravel中的路线

Route::delete('/deleteprocess', 'Controller@destroy');

在 JavaScript 中
$.ajax({
url: '/deleteprocess',
type: 'POST',
data:{
'_token': $('meta[name=csrf-token]').attr("content"),
'_method': 'DELETE',
},
success: function(result) {
// Do something with the result
}});

设置类型:POST,
设置 token :_token,
设置方法:_method 为 DELETE,

关于Laravel - 删除 Ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29828654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com