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

标签 php jquery ajax laravel

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

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

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

jquery.min.js:4 DELETE http://localhost/contributions/batches/batch/edit 404 (Not Found)

这是按钮代码

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

奇怪的是,在错误从我的 Controller 的销毁函数返回我的 session 闪存消息后刷新页面

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

405 (METHOD NOT ALLOWED)

有什么想法吗?

最佳答案

经过两天的阅读和思考,我有了一个可行的解决方案:

路线:

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>

Controller :

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

关于php - 通过 AJAX 的 Laravel Route 调用获取错误的 URL 404(未找到),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49037914/

相关文章:

javascript - 如何使用AJAX正确地将表单提交到Flask?

php - Ajax 调用返回整个页面而不仅仅是回显值

php - Apache/PHP 到 Nginx/Tornado/Python

javascript - 如何删除/覆盖标记为 !important 的元素属性的 CSS

javascript - jQuery fadeIn(), fadeOut() 导致性能急剧下降

javascript - Ajax 成功和错误函数未在 jquery 中正确调用

javascript - 通过href提交Form并传递隐藏变量

php - 做出选择后如何显示额外信息?

php - ajax 成功功能不起作用但显示在 firebug 控制台中

javascript - 如何在 for 循环中使用 AJAX