DELETE with AJAX (Jquery) 不适用于 Laravel 5.2


DELETE with AJAX (Jquery) not working with Laravel 5.2

我正在尝试使用 Jquery/Ajax 删除 db 中的一行。

我已经遵循了这个线程,但我无法使其工作。

以下是我的观点:

@foreach($tournaments as $tournament)
<tr id="line" data-id="{{$tournament->id}}">
    <td>
        <a href="{!!   URL::action('TournamentController@edit',  $tournament->id) !!}">{{ $tournament->id }}</a>
    </td>
    <td>{{ $tournament->owner->name}}</td>
    <td class="text-center">
        {!! Form::open(['method' => 'DELETE', 'id' => 'formDeleteTourament', 'action' => ['TournamentController@destroy', $tournament->id]]) !!}
        {!! Form::button( '<i class="glyphicon glyphicon-remove"></i>', ['class' => 'btn text-warning-600 btn-flat btnDeleteTournament', 'data-id' => $tournament->id ] ) !!}
        {!! Form::close() !!}
    </td>
</tr>
@endforeach
<script>
    $(function () {
        $('#.tnDeleteTournament').on('click', function (e) {
            var inputData = $('#formDeleteTourament').serialize();
            var dataId = $('.btnDeleteTournament').attr('data-id');
            console.log(inputData);  // displays: _method=DELETE&_token=tGFhaAr5fVhDXEYL3SaXem3WaTNJlSdFEkaVDe9F
            console.log(dataId); // displays 1
            var $tr = $(this).closest('tr');
            swal({
                title: "Are you sure?",
                text: "You will not be able to recover this Tournament!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#EF5350",
                confirmButtonText: "Yes, delete it!",
                cancelButtonText: "No, cancel pls!",
                closeOnConfirm: false,
                closeOnCancel: false
            },
            function (isConfirm) {
                if (isConfirm) {
                    e.preventDefault();
                    $.ajax(
                        {
                            type: 'POST', // I tried with DELETE too
                            url: '{{ url('/tournaments/') }}' + dataId,
                            data: inputData,
                            success: function (msg) {
                                console.log('success'); // It triggers!
                                $tr.find('td').fadeOut(1000, function () {
                                    $tr.remove();
                                });
                                swal({
                                    title: "Deleted!",
                                    text: "Tournament has been deleted.",
                                    confirmButtonColor: "#66BB6A",
                                    type: "success"
                                });
                            },
                            error: function () {
                                swal("Oops", "We couldn't connect to the server!", "error");
                            }
                        }
                    )

                }
                else {
                    swal({
                        title: "Cancelled",
                        text: "Your Tournament is safe :)",
                        confirmButtonColor: "#2196F3",
                        type: "error"
                    });
                }
            });
        });
    });

这是我的委托人:

public function destroy(Tournament $tournament)
{
    $tournament->delete();
    return response(['msg' => 'Product deleted', 'status' => 'success']);

}

以前,我让它在没有 AJAX 的情况下工作,所以路由是可以的。

一切正常(模态显示),但它不会删除我的锦标赛

我是JS/JQuery的新手,所以我不知道发生了什么。

知道如何解决它吗?

您需要更改所有引用以使用class作为选择器。我只是猜测Laravel的东西,也让你的id是唯一的(我附加了锦标赛ID),但它都需要更改为class

猜这个,不熟悉语法

@foreach($tournaments as $tournament)
<tr id="line{{$tournament->id}}" data-id="{{$tournament->id}}" class="parentTr">
    <td>
        <a href="{!!   URL::action('TournamentController@edit',  $tournament->id) !!}">{{ $tournament->id }}</a>
    </td>
    <td>{{ $tournament->owner->name}}</td>
    <td class="text-center">
        {!! Form::open(['method' => 'DELETE', 'class' => 'formDeleteTourament', 'action' => ['TournamentController@destroy', $tournament->id]]) !!}
        {!! Form::button( '<i class="glyphicon glyphicon-remove"></i>', ['class' => 'btn text-warning-600 btn-flat btnDeleteTournament', 'data-id' => $tournament->id ] ) !!}
        {!! Form::close() !!}
    </td>
</tr>
@endforeach

Javascript更改

<script>
$(function () {
    $('.btnDeleteTournament').on('click', function (e) {
        var inputData   =   $(this).parents('.formDeleteTourament').serialize();
        var dataId      =   $(this).data('id');
        var $tr         =   $(this).closest('tr');
        swal({
            title: "Are you sure?",
            text: "You will not be able to recover this Tournament!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#EF5350",
            confirmButtonText: "Yes, delete it!",
            cancelButtonText: "No, cancel pls!",
            closeOnConfirm: false,
            closeOnCancel: false
        },
        function (isConfirm) {
            if (isConfirm) {
                e.preventDefault();
                $.ajax({
                        // I don't you know if you have to escape the inner
                        // quotes or not, that is a framework syntax thing
                        url: '{{ url('/tournaments/') }}',
                        type: 'POST', 
                        // Just include the id in a post data object
                        data: { form: inputData, id: dataId },
                        success: function (msg)
                            {
                                console.log(msg);
                                $tr.find('td').fadeOut(1000, function() {
                                    $tr.remove();
                                });
                                swal({
                                    title: "Deleted!",
                                    text: "Tournament has been deleted.",
                                    confirmButtonColor: "#66BB6A",
                                    type: "success"
                                });
                            },
                        error: function()
                            {
                                swal("Oops", "We couldn't connect to the server!", "error");
                            }
                    });
            }
            else {
                swal({
                    title: "Cancelled",
                    text: "Your Tournament is safe :)",
                    confirmButtonColor: "#2196F3",
                    type: "error"
                });
            }
        });
    });
});
</script>