ajax - 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",
});
});
作为参考,我的屏幕截图显示了警报,显示删除功能如下:
另一个参考是我的 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>
推荐阅读
- c++ - 为什么来自 (JNIenv*)->GetStaticMethodID 的“java.lang.NoSuchMethodError:没有静态方法”?
- powershell - Powershell 确定活动以太网适配器和他的 MAC
- ios - firebase_messaging 版本 3.0.0 无法使用 IOS 构建
- laravel - OrderBy 关系的关系列
- maven - 缺少 gradle 'dependencies.dependency.version'
- javascript - 将循环结构转换为 JSON(循环发生在哪里?)
- java - 用整数创建 pojo 的对象
- javascript - React-Table 动态表头怎么做?
- c - c中的内联汇编以获取当前时间、用户名和机器名
- .htaccess - IIS htaccess 规则转换器仅导入 1 条规则