为什么不允许在jquery数据表服务器端处理ajax中使用成功
Why not allowed using success in jquery datatable server side processing ajax?
我正在使用 asp.net mvc5并尝试使用jquery datatable插件在服务器端处理。服务器端处理教程显示了从服务器返回结果的格式。但是我的项目的区别在于我无法从服务器发送"数据"的类型化数组。我正在将整个正文作为带有所有 html 标签的字符串发送。我的数据表代码如下。
var e = t.DataTable({processing: true,
serverSide: true,
info: true,
stateSave: true,
PaginationType: "full_numbers",
ajax:{
"url": '@Url.Action("AjaxGetJsonData","Define",new { tablename= @HttpContext.Current.Request.QueryString["tablename"] })',
"type": "GET",
"success": function (result) {
console.log(result);
$("#sample_1").find("tbody").html(result.data);
$("#sample_1_processing").hide();
Init();
//var oSettings = $("#sample_1").dataTable().fnSettings();
//oSettings.iTotalRecords = result.recordsTotal;
}
}
ajax 的结果如下所示,
Object {draw: 1, iTotalRecords: 25000, iTotalDisplayRecords: 0, data: Array[1]}
数据就像
<tr><td></td><td></td><td></td><td></td></tr>
因为该视图对于许多表都是通用的,并且我应该控制许多情况。因此,我在服务器端使用StringBuilder。如果我把成功放在 ajax 上,分页元素就会消失在数据表的底部。为什么不允许在 ajax 中使用成功?我有数据表的所有功能,有没有办法手动设置像 iTotalRecords 这样的功能?我知道这里不是数据表论坛。我很抱歉,但我花了很多时间,找不到解决方案。我想手动处理 ajax 中数据表的所有功能成功。我正在使用最新版本的数据表。
我最后解决了我的问题。有一些有趣的事情,但我现在可以利用成功。
var e = t.DataTable({
processing: true,
serverSide: true,
info: true,
stateSave: true,
PaginationType: "full_numbers",
"sAjaxSource": '@Url.Action("AjaxGetJsonData","Define")',
"fnServerData": function (sSource, aoData, fnCallback) {
aoData.push({ "name": "tablename", "value": $("#temprory").val() });
console.log(aoData);
$.ajax({
url: sSource,
type: "POST",
data: aoData,
success: function (msg) {
fnCallback(msg);
console.log(msg);
$("#sample_1").find("tbody").html(msg.data);
$("#sample_1_processing").hide();
Init();
}
});
}
有趣的是,如果删除 fnCallback(msg),则包含分页的数据表的以下部分将消失。我不知道它到底做了什么,但这解决了我的问题。
jQuery数据表被编码为使用ajax中的成功回调,如果你截获它,它会中断。源
你也可以使用 jQuery ajax dataFilter 回调。
$('table[id=entries]').DataTable({
processing: true,
serverSide: true,
ajax: {
type: 'POST',
url: 'http://example.com/entries',
dataFilter: function(response) {
var json_response = JSON.parse(response);
if (json_response.recordsTotal) {
// There're entries;
}else{
// There're no entries;
}
return response;
},
error: function (xhr) {
console.error(xhr.responseJSON);
}
}
});
注意:在 dataFilter 回调中返回字符串,而不是 json。
相关文章:
- 在ajax成功后,cluetip不适用于首次点击活动元素
- jQuery成功函数中的ajax成功函数
- jQuery触发器点击未从AJAX成功工作
- 执行ajax成功函数-jQUERY
- 如何从servlet获取数据到ajax成功
- JQuery FullCalendar在从ajax成功调用rerenderEvents时遇到问题
- ajax成功地将数组中的数据放入表中各自的输入中
- 如何从ajax成功回调函数中读取javascript变量
- AJAX成功回调-执行javascript时出现问题
- Javascript等待ajax成功后再执行代码
- 挂钩到Rails内置远程:true'ajax:成功'事件
- 当使用命名函数作为jquery ajax成功回调时,我需要括号吗
- jQuery Ajax成功提交条件问题
- 显示Ajax成功的消息
- 如何在jquery ajax成功html响应中找到输入隐藏元素
- Ajax成功上的Phonegap WebSQL事务不起作用
- 在AJAX成功响应中执行Javascript代码(在单独的文件中)
- ajax成功响应后如何更改html类
- 使用GET方法通过AJAX成功提交表单后更改url
- 无法使用ajax成功登录网页