Laravel 5.5 通过单击 ajax 调用删除项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46341260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.5 delete item with ajax call on click
提问by Chris
I am trying to delete a model item via an ajax call when you click on an icon. Without an ajax call and just with a form everything works great.
当您单击图标时,我试图通过 ajax 调用删除模型项。没有 ajax 调用,只有一个表单,一切都很好。
This exception is thrown when I look in my network tab of my chrome dev tools
当我查看 chrome 开发工具的网络选项卡时抛出此异常
"Symfony\Component\HttpKernel\Exception\HttpException"
“Symfony\Component\HttpKernel\Exception\HttpException”
This is my icon:
这是我的图标:
<i class="fa fa-trash-o deletebtn" aria-hidden="true" data-pointid="<?php echo $damagePoint->id ?>"></i>
My ajax call:
我的 ajax 调用:
$(".deletebtn").click(function(ev){
let pointid = $(this).attr("data-pointid");
$.ajax({
url: '/pointdelete/' + pointid,
type: 'delete',
success: function (response) {
}
});
})
My route:
我的路线:
Route::delete('pointdelete/{id}', 'DamagePointController@delete');
My controller method
我的控制器方法
public function delete($id)
{
$todo = DamagePoint::findOrFail($id);
$todo->delete();
return back();
}
回答by iCoders
if you are using delete route is like similar to post.Here is the sample code.you can change as per your need
如果您使用删除路由类似于 post.Here 是示例代码。您可以根据需要更改
$(".deletebtn").click(function(ev){
let pointid = $(this).attr("data-pointid");
$.ajax({
type: 'DELETE',
url: '/pointdelete',
dataType: 'json',
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
data: {id:pointid,"_token": "{{ csrf_token() }}"},
success: function (data) {
alert('success');
},
error: function (data) {
alert(data);
}
});
});
Route
路线
Route::delete('pointdelete','DamagePointController@delete');
controller
控制器
public function delete(Request $request){
if(isset($request->id)){
$todo = DamagePoint::findOrFail($request->id);
$todo->delete();
return 'success';
}
}
回答by Tarun modi
Please check below code:
请检查以下代码:
Route::delete('pointdelete',['as' => 'point.delete','uses' => 'DamagePointController@delete']);
<button class="fa fa-trash-o deletebtn" aria-hidden="true" onclick="delete({{ $damagePoint->id }}"></button>
<script type="text/javascript">
function delete(id){
var _token = "{{ csrf_token() }}";
$.ajax({
url : "{{URL::route('your-delete-route')}}",
type : 'delete',
dataType : 'json',
data : {
'id': id,
'_token':_token
},
beforeSend : function() {
},
complete : function() {
},
success : function(resp)
{
console.log(resp);
}
});
}
</script>
public function delete(Request $request,$id)
{
DamagePoint::destroy($id);
return response()->json(['success' => true],200);
}
回答by Dhaval Purohit
Try this kind of ajax call
试试这种ajax调用
$(".deletebtn").click(function(ev){
let pointid = $(this).attr("data-pointid");
$.ajax({
url: '/pointdelete/' + pointid,
data : {'_method':'delete','_token':'your csrf token'},
//type: 'delete',
type:'post',
success: function (response) {
}
});
})
Also, verify that what URL is called in request header information in your chrome developer tools.
you just don't need a form try to put your token as {{ csrf_token() }}
in ajax.
此外,请验证在您的 chrome 开发人员工具中的请求标头信息中调用了什么 URL。您只是不需要像{{ csrf_token() }}
在 ajax 中那样尝试将您的令牌放入表单。
回答by Vikas Chauhan
Recently I got the same error and none of above solutions worked for me in laravel 5.7
最近我遇到了同样的错误,并且上述解决方案在 laravel 5.7 中都不适合我
I had code something like -
我有类似的代码 -
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type:'POST',
async: false,
url:validUrl,
data:{id:1},
success:function(response){
if(response && response.error) {
//show error
} else {
// success
}
}
});
Then I had to set csrf token first using setup method - changed code is something like -
然后我必须首先使用设置方法设置 csrf 令牌 - 更改的代码类似于 -
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type:'POST',
async: false,
url:validUrl,
data:{id:1},
success:function(response){
if(response && response.error) {
//show error
} else {
// success
}
}
});