Laravel 5.5删除带有Ajax调用的项目 [英] Laravel 5.5 delete item with ajax call on click

查看:37
本文介绍了Laravel 5.5删除带有Ajax调用的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您单击图标时,我试图通过ajax调用删除模型项.没有ajax调用,只需表单,一切都很好.

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.

当我在chrome开发工具的网络"标签中查看时会抛出此异常

This exception is thrown when I look in my network tab of my chrome dev tools

"Symfony \ Component \ HttpKernel \ Exception \ HttpException"

"Symfony\Component\HttpKernel\Exception\HttpException"

这是我的图标

<i class="fa fa-trash-o deletebtn" aria-hidden="true" data-pointid="<?php echo $damagePoint->id ?>"></i>

我的ajax呼叫:

$(".deletebtn").click(function(ev){
    let pointid = $(this).attr("data-pointid");

    $.ajax({
        url: '/pointdelete/' + pointid,
        type: 'delete',
        success: function (response) {

        }
    });
})

我的路线:

Route::delete('pointdelete/{id}', 'DamagePointController@delete');

我的控制器方法

public function delete($id)
{
    $todo = DamagePoint::findOrFail($id);
    $todo->delete();

    return back();
}

推荐答案

如果您使用的是删除路由,则类似于post.这是示例代码.您可以根据需要进行更改

if you are using delete route is like similar to post.Here is the sample code.you can change as per your need

$(".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::delete('pointdelete','DamagePointController@delete');

控制器

 public function delete(Request $request){

        if(isset($request->id)){
              $todo = DamagePoint::findOrFail($request->id);
              $todo->delete();
              return 'success';
        }
 }

这篇关于Laravel 5.5删除带有Ajax调用的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆