Dynamically adding rows to datatable using ajax and getting JSON Parse error maximum length exceeded
Dynamically adding rows to datatable using ajax and getting JSON Parse error maximum length exceeded
I am populating multiple Jquery Datatables through a ajax call. I get an error when parsing a response jQuery.parseJSON.
In the web service method, Data is being returned as a string after serializing it using System.Web.Script.Serialization.JavaScriptSerializer . Also, the max length property is set to MaxJsonLength = Int32.MaxValue .
All works fine if the amount of data is not too large.
How to avoid the max length error? Is there a way to populate datatable without parsing a response ?
Here is the Error Message
"Message":"Maximum length exceeded.","StackTrace":" at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
This is how the tables are being setup
if ($.fn.dataTable.isDataTable('#tblSPlate')) {
tblSPlate = $('#tblSPlate').DataTable();
}
else {
tblSPlate = $('#tblSPlate').DataTable({
"processing": true,
//"serverSide": true,
"bPaginate": false,
"bFilter": false,
"bInfo": false,
"scrollY": "300px",
"scrollCollapse": true,
//"bDestroy": true,
"columns": [
{ "data": "first_name" },
{ "data": "second_name" },
{ "data": "DOB" },
{ "data": "Title" }
]
});
}
$.ajax({
type: "POST",
url: "MyService.asmx/GetData",
contentType: "application/json",
dataType: "json",
data: "{ regDate: '" + reg_date + "', plate: '" + sample + "', type: '" + type + "' , status: '" + sample_status + "'}",
success: function (response) {
response = jQuery.parseJSON(response); // JSON.parse(response);
tblSPlate.clear();
tblSPlate.rows.add(response[0]).draw();
tblJPlate.clear();
tblJPlate.rows.add(response[1]).draw();
tblPPlate.clear();
tblPPlate.rows.add(response[2]).draw();
}
Answers
That's not a DataTables error. It's specific to Javascript Serialization.
You could try StackOverflow for solutions.
Is there a way to add rows to a table without parsing ajax response (jQuery.parseJSON)?
You can add rows
rows.add()
- the examples on that page should help,Colin