Javascript 数据表 - ajax 成功
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36545411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Datatable - ajax success
提问by rad11
When I add success function DataTable not fill up automaticly rows in table. When I remove success function everything is all right and datatable fill correctly data in table. I want to catch in response using getAccessMessageWithStatus message by status but if I do it like this datatable no filling rows. How I can do that?
当我添加成功函数 DataTable 时,不会自动填充表中的行。当我删除成功功能时,一切正常,数据表在表中正确填充数据。我想通过状态使用 getAccessMessageWithStatus 消息来捕捉响应,但是如果我像这个数据表那样做,就没有填充行。我怎么能做到这一点?
$('#' + datatableName).DataTable({
destroy: true,
'bProcessing': false,
'bServerSide': true,
'ajax': {
'url': URL,
'data': filters,
beforeSend: function() {
loader.popup('show');
},
success: function(response) {
getAccessMessageWithStatus(response);
},
complete: function() {
$listContainer.show();
$containerChoiseColumnsFilter.show();
$(".containerRaportButtons").show();
getLastSearches();
getUses();
loader.popup('hide');
}
},
'sServerMethod': "POST",
'columns': columns,
'order': order,
'responsive': true
});
Answers:
答案:
success: function(response) {
getAccessMessageWithStatus(response);
},
Or:
或者:
"dataSrc": function(response) {
if (response.status == false) {
alert(response.msg);
return [];
}
return response.aaData;
},
回答by Dharmesh Goswami
Remove ";" after the function name into the code.
消除 ”;” 将函数名写入代码后。
success: function (response) {
getAccessMessageWithStatus(response)
},
回答by Carlos Huamani
There is an event from DatatTable called 'xhr.dt'. You can use it in that way.
DatatTable 中有一个名为'xhr.dt'的事件。你可以这样使用它。
$('#' + datatableName).on('xhr.dt', function(e, settings, json, xhr){
getAccessMessageWithStatus(json);
}).DataTable({
destroy: true,
'bProcessing': false,
'bServerSide': true,
'ajax':
{
'url': URL,
'data': filters,
beforeSend: function () {
loader.popup('show');
},
complete: function () {
$listContainer.show();
$containerChoiseColumnsFilter.show();
$(".containerRaportButtons").show();
getLastSearches();
getUses();
loader.popup('hide');
}
}
});
You shouldn't use success from ajax attribute because you will overwrite the success function from DataTable. See this piece of code from query.dataTables.js
您不应该使用来自 ajax 属性的成功,因为您将覆盖来自 DataTable 的成功函数。请参阅 query.dataTables.js 中的这段代码
"success": function (json) {
var error = json.error || json.sError;
if ( error ) {
_fnLog( oSettings, 0, error );
}
oSettings.json = json;
callback( json );
}
You can notice that they have a callback inside this function. This callback triggers the function _fnCallbackFire and this call the event xhr.dt
你可以注意到他们在这个函数中有一个回调。此回调触发函数 _fnCallbackFire 并调用事件 xhr.dt
For more information, go to this page https://datatables.net/reference/event/xhr
有关更多信息,请转到此页面 https://datatables.net/reference/event/xhr
回答by Matheus Miranda
You can use it this way:
你可以这样使用它:
"complete": function (json, type) { //type return "success" or "parsererror"
if (type == "parsererror") {
alert("parsererror");
}
...
}