Datatable Warning: Json data could not be parsed error

Datatable Warning: Json data could not be parsed error

sagar25_25sagar25_25 Posts: 10Questions: 0Answers: 0
edited August 2012 in DataTables 1.9
I am trying send data from ym servlet to jsp. I am getting above error. Please let me know what is wrong in this....but could not find solution...

Data being passed from servlet :

{"iTotalRecords":"5","iTotalDisplayRecords":"5","aaData":[["test","browser","platform","version","grade"]]}

Jsp ajax code :

$(document).ready(function() {
var oTable = $('#example').dataTable( {
"bProcessing": true,
"sAjaxSource": "http://localhost:7001/basisCalculatorObjects.do",
"aoColumns": [
{ "mData": "name" },
{ "mData": "address" },
{ "mData": "town" },
{ "mData": "state" },
{ "mData": "zip" }]
} );
} );

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    See http://datatables.net/faqs#json

    Allan
  • sagar25_25sagar25_25 Posts: 10Questions: 0Answers: 0
    Alan i again rewrote code ...it works when i use javascript array option..but when i dynamically get aaData it is failing..

    Below is my javascript array example code which works.

    $(document).ready(function() { $('#demo').html( '' );
    $('#example').dataTable( { "aaData": [
    [ "Trident", "Internet Explorer 6.0", "Win 98+", 6, "A" ],
    [ "Trident", "Internet Explorer 7.0", "Win XP SP2+", 7, "A" ]
    ],
    "aoColumns": [ { "sTitle": "Engine" },
    { "sTitle": "Browser" },
    { "sTitle": "Platform" },
    { "sTitle": "Version", "sClass": "center" },
    { "sTitle": "Grade", "sClass": "center", "fnRender": function(obj) {
    var sReturn = obj.aData[ obj.iDataColumn ];
    if ( sReturn == "A" ) {
    sReturn = "A"; }
    return sReturn; } }
    ] } );
    } );





    but when i dynamically get value of aadata from server it is giving same json parser error. I have checked my json output is valid.

    This is my jsp portion

    $('#example').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "http://localhost:7001/canada/internal/basisCalculatorObjects.do",
    "aoColumns": [ { "sTitle": "Engine" },
    { "sTitle": "Browser" },
    { "sTitle": "Platform" },
    { "sTitle": "Version", "sClass": "center" },
    { "sTitle": "Grade", "sClass": "center", "fnRender": function(obj) {
    var sReturn = obj.aData[ obj.iDataColumn ];
    if ( sReturn == "A" ) {
    sReturn = "A"; }
    return sReturn; } }
    ] } );
    } );

    This is my json data

    {"sEcho":1,"iTotalRecords":"57","iTotalDisplayRecords":"57","aaData":[["test","40","wilton",123,"11111"]]}
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    That http://localhost:7001 looks iffy to me. Unless you are running from that specific domain name (127.0.0.1 isn't the same in this case) you will be running into XSS issues.

    A link would be most useful or at least running it through the DataTables debugger.

    Allan
  • sagar25_25sagar25_25 Posts: 10Questions: 0Answers: 0
    Alan thanks for the reply . I have used debugger. the code is ewapoj

    my json data is below

    {"sEcho":"1","iTotalRecords":3,"iTotalDisplayRecords":3,"aaData":[["Saurabh","Avon","Hartford"],["Saurabh","Avon","Hartford"],["Saurabh","Avon","Hartford"]]}

    now the error i am getting is oinit.aaData.length is null or not an object jquery.datatables.js line 6720.

    Result is still same no data on page.

    my jsp code...












    $.ajax({ type: "POST", url: "http://localhost:7001/canada/internal/basisCalculatorObjects.do",
    contentType: "application/json; charset=utf-8", dataType: "json",
    success: function(data) { $('#companies').dataTable({ "aaData": eval(data.d) }); } });





    localhost:7001 is my local server code so that is correct.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    It really would be useful if you would run it through the debugger or put it online please.

    However:

    > eval(data.d)

    What is data.d - is that your JSON from the server? In which case you are feeding it:

    [code]
    aaData: {"sEcho":1,"iTotalRecords":"57","iTotalDisplayRecords":"57","aaData":[["test","40","wilton",123,"11111"]]}
    [/code]

    which isn't right.

    Allan
  • sagar25_25sagar25_25 Posts: 10Questions: 0Answers: 0
    Alan i forgot to metnion if you notice my above post I am using $.ajax ....the original $(document).ready(function() {
    $("#companies").dataTable ajax call was not working for me. However in .ajax i noticed i am not getting sEcho value...so i am setting it as 1 in my action class.
  • sagar25_25sagar25_25 Posts: 10Questions: 0Answers: 0
    Alan,
    This is how i am setting in server

    jsonResponse.add("aaData", data);

    response.setContentType("application/Json");
    response.getWriter().print(jsonResponse.toString());

    so in my jsp

    $.ajax({ type: "POST", url: "http://localhost:7001/canada/internal/basisCalculatorObjects.do",
    contentType: "application/json; charset=utf-8", dataType: "json",
    success: function(data) { $('#companies').dataTable({ "aaData": data

    });
    });

    should it be just data.....
  • sagar25_25sagar25_25 Posts: 10Questions: 0Answers: 0
    Alan i have changed data.d to data

    $.ajax({ type: "POST", url: "http://localhost:7001/canada/internal/basisCalculatorObjects.do",
    contentType: "application/json; charset=utf-8", dataType: "json",
    success: function(data) { $('#companies').dataTable({ "aaData": data

    });
    });


    now i am getting no data available in table.

    the debugger code is ewuwac
  • sagar25_25sagar25_25 Posts: 10Questions: 0Answers: 0
    Alan thanks for all your help. I am able to get data.
This discussion has been closed.