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 没有刷新我的表?

更新:

  • 我知道 location.reload();,但添加它违背了目的。
  • 数据确实被删除了,但我必须手动刷新整个页面才能看到更改。

解决方法

你可以在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>