Laravel 5.5在点击时用ajax调用删除项目

时间:2017-09-21 10:14:56

标签: php jquery ajax laravel

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

当我查看chrome dev工具的网络选项卡时,会抛出此异常

  

“的Symfony \元器件\ HttpKernel \异常\ 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();
}

4 个答案:

答案 0 :(得分:6)

如果您使用删除路由类似于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::delete('pointdelete','DamagePointController@delete');

控制器

 public function delete(Request $request){

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

答案 1 :(得分:4)

请检查以下代码:

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);
}

答案 2 :(得分:1)

尝试这种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) {

        }
    });
})

此外,验证chrome开发人员工具中请求标头信息中调用的URL。 你只需要一个表单就可以将你的令牌{{ csrf_token() }}放在ajax中。

答案 3 :(得分:0)

最近我遇到了相同的错误,上述所有解决方案在laravel 5.7中均不起作用

我有类似-

的代码
$.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  
            }
        }

     });

然后我必须首先使用设置方法设置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  
            }
        }

     });