客户端上的脚本,该脚本具有函数调用,用于在SignalR.connection.Start上初始化Datatable,并在and依赖项更改时从集线器获取UpdatedData。
$(function () {
// Reference the hub.
var hubNotif = $.connection.RunningPOHub;
// Start the connection.
$.connection.hub.start().done(function () {
getAll();
});
// Notify while anyChanges.
hubNotif.client.updatedData = function (data) {
getUpdatedData(data);
}
});
function getAll() {
$('#demoGrid').dataTable({
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"filter": true, // this is for disable filter (search box)
"orderMulti": false, // for disable multiple column at once
"pageLength": 2,
"responsive": true,
"ajax": {
"url": "/RunningPO/GetData",
"type": "POST",
"datatype": "json",
"dataSrc": ""
},
"columns": [
{ "data": "Unit", "name": "Unit", "autoWidth": true },
{ "data": "PO_NO", "name": "PO_NO", "autoWidth": true },
{ "data": "QUANTITY_AV", "title": "Quantity", "name": "QUANTITY_AV", "autoWidth": true }
],
});
}
function getUpdatedData(data) {
var jdata= JSON.stringify(data);
alert(JSON.stringify(data));//Please see the attached screenshot and JSON
//response shown below.
debugger;
$('#demoGrid').dataTable({
"destroy": true,
"processing": true, // for show progress bar
"serverSide": true, // for process server side
"filter": true, // this is for disable filter (search box)
"orderMulti": false, // for disable multiple column at once
"pageLength": 2,
"responsive": true,
"data": jdata,
"columns": [
{ "data": "Unit", "name": "Unit", "autoWidth": true },
{ "data": "PO_NO", "name": "PO_NO", "autoWidth": true },
{ "data": "QUANTITY_AV", "title": "Quantity", "name": "QUANTITY_AV", "autoWidth": true }
],
});
}从服务器获得的JSON响应。

[
{"PO_NO":"237","Unit":"TSD-1","QUANTITY_AV":"123.45"},
{"PO_NO":"5","Unit":"TSD-2","QUANTITY_AV":"765.90"},
{"PO_NO":"25","Unit":"TSD-3","QUANTITY_AV":"78998"}
]数据在初始的getAll()函数中加载(如下所附),其中数据是从对Controller/ActionMethod的内部ajax调用中接收的。

当响应通过SignalR集线器从服务器接收并加载到getUpdatedData(data)函数中的$('#demoGrid').dataTable({})时。引发错误。
无效JSON数据
发布于 2018-07-26 12:30:57
你的儿子肯定是这样的:
{
data: [
{"PO_NO":"237","Unit":"TSD-1","QUANTITY_AV":"123.45"},
{"PO_NO":"5","Unit":"TSD-2","QUANTITY_AV":"765.90"},
{"PO_NO":"25","Unit":"TSD-3","QUANTITY_AV":"78998"}
]
}数据json必须包含数据,但必须包含您的列表。
https://stackoverflow.com/questions/51537543
复制相似问题