I am trying to follow an example of adding data to a datatables.net datatable using a JSON response based on this example https://www.datatables.net/examples/ajax/objects.html.
我试图按照一个例子,使用基于此示例https://www.datatables.net/examples/ajax/objects.html的JSON响应将数据添加到datatables.net数据表。
I am using an AJAX call to get a JSON response from a database.
我正在使用AJAX调用从数据库中获取JSON响应。
I am getting the data and then using NewtonSoft JSON.Net to convert a datatable into a JSON array as per the code below
我正在获取数据然后使用NewtonSoft JSON.Net将数据表转换为JSON数组,如下面的代码
string jsonResult = null;
jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(dt);
jsonResult = jsonResult.Replace("[{", "{\"data\" :[{").Replace("}]", "}]}");
return jsonResult;
This is being successfully called from an AJAX call in a separate javascript file as per the next code snippet
根据下一个代码片段,这是在一个单独的javascript文件中从AJAX调用成功调用的
$.ajax({
type: "POST",
url: "TeamChecks.aspx/GetDataTables",
data: JSON.stringify(params),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
console.log(data.d);
populateTable(data.d, tableId);
},
error: function (data) {
console.log(data);
}
}
Which then passes the returned values to a function that should format enter the data into the datatable accordingly as per the next set of code
然后将返回的值传递给应该格式化的函数,该函数根据下一组代码相应地将数据输入到数据表中
function populateTable(json, tableId) {
try {
var table = $('#table_' + tableId).DataTable({
"processing": true,
"serverSide": true,
"ajax": json,
"columns:": [
{ "data": "CaseHandlerStaffNumber" },
{ "data": "RiskProfileText" },
{ "data": "AssignedCheckerStaffNumber" },
{ "data": "FeedbackUserStaffNumber" },
{ "data": "ComplaintRef" },
{ "data": "ChildComplaintRef" },
{ "data": "CaseTypeText" },
{ "data": "CheckGrade" }
]
});
} catch (e) {
}
}
The problem that I am getting is similar to this question here jquery datatables Ajax-Error / http://datatables.net/tn/7 however, I have tried this users solution and have the same issue.
我得到的问题类似于这里的问题jquery datatables Ajax-Error / http://datatables.net/tn/7但是,我已经尝试过这个用户解决方案并且遇到了同样的问题。
I have followed all the recommended steps as detailed here https://www.datatables.net/manual/tech-notes/7 but do not see anything wrong with the responses that come back that relate to this.
我已经按照https://www.datatables.net/manual/tech-notes/7中详述的所有建议步骤进行了操作,但是没有看到与此相关的回复有任何问题。
I would be grateful if anyone could help me with this as I cannot see a way around the issue at the moment
如果有人能帮助我,我将不胜感激,因为我现在无法找到解决问题的方法
thanks
Simon
1 个解决方案
#1
0
Basically, you are working against yourself when you have an ajax call to get the data but then add ajax to the datatables definition.
基本上,当您进行ajax调用以获取数据但随后将ajax添加到datatables定义时,您正在反对自己。
Set up web method; note that when you do it like this, the data gets serialized twice, once by you and once by the scripting services module.
设置web方法;请注意,当您这样做时,数据将被序列化两次,一次由您执行,一次由脚本服务模块执行。
[WebMethod]
public string getTheData(String params){
SomeDataClass dt = getthedata();
return Newtonsoft.Json.JsonConvert.SerializeObject(dt);
}
now the ajax:
现在是ajax:
$(document).ready(function(){
$.ajax({
type: "POST",
url: "TeamChecks.aspx/GetDataTables",
data: JSON.stringify(params),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
// jquery did the first deserialization
// here is where the second deserization happens.
var data = JSON.parse(reponse.d);
populateTable(data, tableId);
},
error: function (data) {
console.log(data);
}
}
});
then your function
那么你的功能
function populateTable(json, tableId) {
var table = $('#table_' + tableId).DataTable({
// in this layout DataTables is expecting a straight array of objects or array of arrays depending on how you are sending the data back
"data": json,
"columns:": [
{ "data": "CaseHandlerStaffNumber" },
{ "data": "RiskProfileText" },
{ "data": "AssignedCheckerStaffNumber" },
{ "data": "FeedbackUserStaffNumber" },
{ "data": "ComplaintRef" },
{ "data": "ChildComplaintRef" },
{ "data": "CaseTypeText" },
{ "data": "CheckGrade" }
]
});
}
#1
0
Basically, you are working against yourself when you have an ajax call to get the data but then add ajax to the datatables definition.
基本上,当您进行ajax调用以获取数据但随后将ajax添加到datatables定义时,您正在反对自己。
Set up web method; note that when you do it like this, the data gets serialized twice, once by you and once by the scripting services module.
设置web方法;请注意,当您这样做时,数据将被序列化两次,一次由您执行,一次由脚本服务模块执行。
[WebMethod]
public string getTheData(String params){
SomeDataClass dt = getthedata();
return Newtonsoft.Json.JsonConvert.SerializeObject(dt);
}
now the ajax:
现在是ajax:
$(document).ready(function(){
$.ajax({
type: "POST",
url: "TeamChecks.aspx/GetDataTables",
data: JSON.stringify(params),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
// jquery did the first deserialization
// here is where the second deserization happens.
var data = JSON.parse(reponse.d);
populateTable(data, tableId);
},
error: function (data) {
console.log(data);
}
}
});
then your function
那么你的功能
function populateTable(json, tableId) {
var table = $('#table_' + tableId).DataTable({
// in this layout DataTables is expecting a straight array of objects or array of arrays depending on how you are sending the data back
"data": json,
"columns:": [
{ "data": "CaseHandlerStaffNumber" },
{ "data": "RiskProfileText" },
{ "data": "AssignedCheckerStaffNumber" },
{ "data": "FeedbackUserStaffNumber" },
{ "data": "ComplaintRef" },
{ "data": "ChildComplaintRef" },
{ "data": "CaseTypeText" },
{ "data": "CheckGrade" }
]
});
}