如何使用laravel 5通过jQuery ajax删除记录?

时间:2019-05-13 09:35:21

标签: php jquery ajax laravel-5.4

resources / views / emp-list.blade.php

<script>
    $(document).ready(function(){
        $(".delete").click(function(e){
            e.preventDefault();
            idd = this.id;
            $.ajax({
                type:"post",
                data:{"idd":idd, "_token":"{{csrf_token()}}",'_method': 'DELETE',},
                url:"{{URL::to('delete_emp')}}",
                success:function(res){
                    location.reload();
                }
            });
        });
    });
</script>

<table class="table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        @foreach($result as $row)
        <tr>
            <td>{{ $row->fname }}</td>
            <td>
                <a href="javascript:void(0)" id="{{ $row->id }}" class="btn btn-danger delete" onclick="return confirm('are you sure')">Delete</a>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>

api / http / controller / UserController.php

public function delete_emp(Request $request)
{
    $id = $request->input('idd');
    DB::delete('delete from emp where id = ?',[$id]);
}

routes / web.php

Route::get('delete_emp','UserController@delete_emp');

我是laravel 5的新手现在我要在这里单击删除按钮以删除特定记录,但是当前单击删除按钮时会发生什么,我的console中出现错误。那么,我该如何解决这个问题呢?请帮我。

谢谢

0 个答案:

没有答案