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

sdyson3sdyson3 Posts: 4Questions: 3Answers: 0

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

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    That's not a DataTables error. It's specific to Javascript Serialization.
    You could try StackOverflow for solutions.

  • sdyson3sdyson3 Posts: 4Questions: 3Answers: 0

    Is there a way to add rows to a table without parsing ajax response (jQuery.parseJSON)?

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    You can add rows rows.add() - the examples on that page should help,

    Colin

This discussion has been closed.