gpt4 book ai didi

jquery - 数据表 fnDraw 发出多个 ajax 请求

转载 作者:行者123 更新时间:2023-12-01 05:00:22 27 4
gpt4 key购买 nike

当我手动重绘(通过单击按钮)时,我的 jquery 数据表似乎发布了两次 - 如何防止数据表执行此操作?

这是我的客户端代码:

$(document).ready(function () {
dataTable = $('#Results').dataTable({
"bLengthChange": true,
"bFilter": false,
"bSort": true,
"bInfo": true,
"bAutoWidth": false,
"sPaginationType": "bootstrap",
"sDom": "ftrpli",
"bServerSide": true,
// "sAjaxSource": "AjaxControllerAction",
"fnServerParams": function (aoData) {
aoData.push({ "name": "UserId", "value": $('#UserId').val() });
aoData.push({ "name": "Data", "value": $('#Data').val() });
aoData.push({ "name": "Location", "value": $('#Location').val() });
},
"fnServerData": function (sSource, aoData, fnCallback) {
showProgress();

var headers = {};
headers['__RequestVerificationToken'] = token;
alert('zzzz');

$.ajax({
dataType: 'json',
type: 'POST',
//url: sSource,
url: 'AjaxControllerAction',
data: aoData,
headers: headers,
success: function (json) {
hideProgress();

fnCallback(json);
},
error: function (json) {
hideProgress();
}
});
},
"fnDrawCallback": function () { hideProgress(); },
"bProcessing": false,
"aoColumns": [
{ "sName": "NAME",
"fnRender": function (oObj) {
return '<a href="Details/' +
oObj.aData[0] + '">' + oObj.aData[0] + '</a>';
}
},
{ "sName": "DATA" },
{ "sName": "DATA1" },
{ "sName": "DATA2" },
]
});
}
);

$("#SearchForm").on("submit", function (event) {
event.preventDefault();
// this sends the request twice ??
dataTable.fnDraw();
});

我知道它两次点击我的 AjaxControllerAction,因为使用了两个 firebug(我看到 xmlhttprequest 两次),并且它通过设置断点两次点击我的操作。我找不到它这样做的任何理由。谢谢!

最佳答案

我的解决方案

我使用了数据表中的参数: iDisplayStart -> 显示当前数据集中的起点。

Step 1: Create a global variable to persiste datas.

var options = {
jsonResultDataTableFromServer: {},
iDisplayStart: -1,
iDisplayLength : -1
};

Step 2: Change ajax fnServerData function to:

"fnServerData": function(url, data, callback) {
//data[3] i.e iDisplayStart
//data[4] i.e iDisplayLength
if (options.iDisplayStart != data[3].value || options.iDisplayLength != data[4].value) {
options.iDisplayStart = data[3].value;
options.iDisplayLength = data[4].value;

$.ajax({
"url": url,
"data": data,
//"success": callback,
"success": function(json) {
options.jsonResultDataTableFromServer = json;
callback(json);
},
"contentType": "application/x-www-form-urlencoded; charset=utf-8",
"dataType": "json",
"type": "POST",
"error": function() {
alert("DataTables warning: JSON data from server failed to load or be parsed. " +
"This is most likely to be caused by a JSON formatting error.");
}
});
} else {
options.jsonResultDataTableFromServer.sEcho = eval(options.jsonResultDataTableFromServer.sEcho) + 1;
callback(options.jsonResultDataTableFromServer);
}

return false;
},

关于jquery - 数据表 fnDraw 发出多个 ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10805269/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com