首页 > 解决方案 > Laravel/Ajax 删除功能成功后不刷新我的表

问题描述

当我在我的删除 ajax 中添加我的函数fetchcategory();时,我的表没有刷新:

        $(document).on('click', '.delete_category_btn', function (e) {
            e.preventDefault();
            var cat_id = $('#delete_cat_id').val();
            alert('Category Deleted!');
            // location.reload();  
            $('#deleteCategoryModal').modal('hide'); 
            fetchcategory();
            
            //token taken from laravel documentation
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type: "DELETE",
                url: "/clinical/bbr-category-configuration-delete/"+cat_id,
                dataType: "dataType",
            });
        });

作为参考,我的屏幕截图显示了警报,显示删除功能如下:

1

另一个参考是我的 add 函数在添加新表后重新加载页面:

            $.ajax({
                type: "POST",
                url: "/clinical/bbr-category-configuration",
                data: category_data,
                dataType: "json",
                success: function (response){
                    console.log(response);
                if(response.status == 400) {
                    $('#category_formCheck').html("");
                    $('#category_formCheck').addClass('alert alert-danger');
                    $.each(response.errors, function (key, err_values) {
                        $('#category_formCheck').append('<li>'+err_values+'</li>');
                    });
                    }
                else  
                  {
                    $('#category_notif').html("");
                    $('#category_notif').addClass('alert alert-success');
                    $('#category_notif').text(response.message);
                    $('#createCategory').modal('hide');
                    fetchcategory();
                    }
                }
            });

我的另一个问题是我试图在里面添加警报和刷新功能:

            $.ajax({
                type: "DELETE",

但他们没有出现,所以我把它们放在里面on(click

为什么我的删除 ajax 没有刷新我的表?

更新:

标签: ajaxlaravelfunctionsql-delete

解决方案


你可以在ajax成功中做到这一点。我相信fetchcategory()方法是将数据填充到表中

<script>
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });


    $(document).on('click', '.delete_category_btn', function (e) {
        e.preventDefault();
        var cat_id = $('#delete_cat_id').val();

        // location.reload();
        $('#deleteCategoryModal').modal('hide');

        $.ajax({
            type: "DELETE",
            url: "/clinical/bbr-category-configuration-delete/"+cat_id,
            dataType: "dataType",
            success: function(data) {
                alert('Category Deleted!');
                fetchcategory();
            },
            error: function() {
                alert('Error occured');
            }
        });
    });
</script>

推荐阅读