通过AJAX的Laravel Route调用获取错误的URL 404(未找到)

问题描述 投票:0回答:2

我在web.php中定义了以下路由

Route::delete('/contributions/contribution/destroy', 'ContributionController@destroy');

在我的刀片文件中,我有一个按钮,单击该按钮会显示sweetalert以确认删除。这可以正常工作,但是当用户按下删除按钮而不是转到Ajax中定义的URL时,它会转到错误的URL并记录以下错误:

jquery.min.js:4删除http://localhost/contributions/batches/batch/edit 404(未找到)

这是按钮代码

<td class="text-center"><button type="button" class="btn btn-danger btn-sm btn-icon btn-destroy" 
                                                    data-batch-id="{{ $batch->id }}" 
                                                    data-id="{{ $contribution->id }}" 
                                                    data-amount="${{ number_format ($contribution->contribution_amount, 2, '.', ',') }}"
                                                    data-contributor="{{ $contribution->first_name.' '.$contribution->last_name }}"
                                                ><i class="fa fa-times"></i></button>
                        </td>

这是我的剧本

<script>

$(document).on('click', '.btn-destroy', function (e) {
    e.preventDefault();
    var batchId = $(this).data('batch-id');
    var id = $(this).data('id');
    var amount = $(this).data('amount');
    var contributor = $(this).data('contributor');
    swal({
            title: "Delete?",
            text: "Do you want to delete contribution for "+ amount +" from "+ contributor +"?",
            type: "error",
            showCancelButton: true,
            confirmButtonClass: 'btn-danger',
            confirmButtonText: "Delete",
            closeOnConfirm: false
        },
        function() {
            $.ajax({
                url: "{{ url('/contributions/contribution/destroy') }}",
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                type: "DELETE",
                data: {batch:batchId, contribution:id},
                success: function (data) {
                    console.log(data);
                    }               
            });
    });
});

奇怪的是,在错误从我的控制器的销毁功能返回会话flash消息后刷新页面

*注意:在我收到之前,我确实在IIS中添加了DELETE动词:

405(不允许的方法)

有任何想法吗?

php jquery ajax laravel
2个回答
0
投票

试试这种风格的ajax

$.ajax({
        url: "{{ url('/contributions/contribution/destroy') }}",
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
       'data':{
         '_method':'delete',
          '_token':'{{ csrf_token() }}',
       },
       'method':'post'
});

0
投票

经过两天的阅读和痛苦,我有一个有效的解决方案:

路线:

Route::delete('/contributions/contribution/destroy', 'ContributionController@destroy');

链接HTML:

<a href="#" class="btn btn-danger btn-sm btn-icon btn-destroy" 
      data-id="{{ $contribution->id }}" 
      data-amount="${{ number_format ($contribution->contribution_amount, 2, '.', ',') }}"
      data-contributor="{{ $contribution->first_name.' '.$contribution->last_name }}"><i class="fa fa-times"></i></a>

jQuery的/ AJAX:

<script>

    $(document).on('click', '.btn-destroy', function (e) {
        e.preventDefault();
        var contributionId = $(this).data('id');
        var amount = $(this).data('amount');
        var contributor = $(this).data('contributor');
        swal({
                title: "Delete?",
                text: "Do you want to delete contribution for "+ amount +" from "+ contributor +"?",
                type: "error",
                showCancelButton: true,
                confirmButtonClass: 'btn-danger',
                confirmButtonText: "Delete",
                closeOnConfirm: false
            },
            function() {
                $.ajax({
                    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                    method: "DELETE",
                    url: "{{ url('/contributions/contribution/destroy') }}",
                    data: {contributionId:contributionId},
                    success: function (msg) {
                        if ( msg.status === 'success' ) {
                            swal("Deleted", msg.msg, "success");
                            setInterval(function() {
                                window.location.reload();
                            }, 3900);
                        }
                    },              
                    error: function(msg) {
                        swal("Error!", "Something went wrong.", "error");
                        console.log(msg.status);
                    }
                });
        });
    }); 

</script>

控制器:

public function destroy(Request $request)

{
    if ( $request->ajax() ) {

        Contribution::find($request->contributionId)->delete();

        return response(['msg' => 'Entry deleted', 'status' => 'success']);
    }
    return response(['msg' => 'Failed deleting the entry', 'status' => 'failed']);        
} 

完美的工作!

Delete swal

Confirmation

© www.soinside.com 2019 - 2024. All rights reserved.