JSON data from server failed could not be parsed -- JSR 286 portlet

JSON data from server failed could not be parsed -- JSR 286 portlet

myahmed29myahmed29 Posts: 3Questions: 0Answers: 0
edited September 2012 in DataTables 1.8
I am trying to load json data from JSR 286 portlet using ajax request from my JSP
[code]


$(document).ready(function() {
$('#messages').dataTable({
"iDisplayLength": 2,
"bLengthChange": false,
"bPaginate" : true,
"bJQueryUI": true,
"bSort": true,
"bInfo": false,
"bFilter": false,
"bProcessing": false,
"sAjaxSource": ""
});
});
[/code]

And the table looks like this...
[code]


Subject
Date/Time





[/code]

The script works fine and the portlet receives call to load data. (so am not worried about call to my JSR 286 portlet). However the problem am facing is with the JSON data my script receives back from portlet. I have also verified the response on jsonlint.com
Here is my portlet code...
[code]String s = ""+
"{" +
"\"aaData\": [" +
"{" +
"\"Trident\"," +
"\"X\"" +
"}," +
"{" +
"\"Trident\"," +
"\"C\"" +
"}" +
"]" +
"}";
resResp.getWriter().print(s);
resResp.getWriter().flush();
resResp.getWriter().close();[/code]

The JSON response I get back is valid and looks like this...
[code]{
"aaData": [
[
"Trident",
"X"
],
[
"Trident",
"C"
]
]
}[/code]

But the datatable fails to validate the JSON data...
[quote]"JSON data from server failed could not be parsed"[/quote]

I am using DataTable v1.8.2

Please advice!

Thanks
Ahmed

Replies

  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin
    If you use Firebug what does the return from the server look list? Also, update to DataTables 1.9 and then use the debugger.

    Allan
  • myahmed29myahmed29 Posts: 3Questions: 0Answers: 0
    edited September 2012
    Ok great its working now post update to DataTables v1.9
    Thanks Allan :) the dataTable stuff is really awesome!
    Here are the changes I made to the jsp.

    1. Added mData to the DataTable initialization code
    [code]$(document).ready(function() {
    $('#query1').dataTable({
    "bPaginate" : true,
    "bJQueryUI": true,
    "bLengthChange": false,
    "bSort": false,
    "bInfo": false,
    "bFilter": false,
    "bProcessing": false,
    "sAjaxSource": "",
    "aoColumns": [
    { "mData": "d1" },
    { "mData": "d2" },
    { "mData": "d3" },
    { "mData": "d4" },
    { "mData": "d5" },
    { "mData": "d6" }
    ]
    });
    });[/code]

    2. Accordingly made changes to the table header
    [code]


    d1
    d2
    d3
    d4
    d5
    d6





    [/code]

    3. Also changes were requried in the json data format at the portlet end
    [code]String s = ""+
    "{" +
    "\"aaData\": [" +
    "{" +
    "\"d1\": \"X\"," +
    "\"d2\": \"X\"," +
    "\"d3\": \"X\"," +
    "\"d4\": \"X\"," +
    "\"d5\": \"X\"," +
    "\"d6\": \"X\"" +
    "}," +
    "{" +
    "\"d1\": \"C\"," +
    "\"d2\": \"C\"," +
    "\"d3\": \"C\"," +
    "\"d4\": \"C\"," +
    "\"d5\": \"C\"," +
    "\"d6\": \"C\"" +
    "}" +
    "]" +
    "}";
    resResp.getWriter().print(s);
    resResp.getWriter().flush();
    resResp.getWriter().close();
    [/code]

    and the json response looks like this...
    [quote]{"aaData": [{"d1": "X","d2": "X","d3": "X","d4": "X","d5": "X","d6": "X"},{"d1": "C","d2": "C","d3": "C","d4": "C","d5": "C","d6": "C"}]}[/quote]

    Thanks again!
  • robertmazzorobertmazzo Posts: 22Questions: 0Answers: 0
    edited September 2012
    I'm having a Json related issue when rendering my DataTable when adding data to my JsonTest variable.

    It works as follows, with only one record but I can't figure out how to ADD to my Json object:

    [code]
    var JsonTest = { "aaData": [] };

    JsonTest =
    {
    "PfId": "1000",
    "Name": "Bob Jr.",
    "ExpType": "Hs Var",
    "Date": "05/01/1967",
    "Term": "term 123",
    "Exposure": "55,000,000"
    };

    var oTable = $('#pftable').dataTable({
    "aaData": [JsonTest],
    "aoColumns":[
    { "mData": "PfId" },
    { "mData": "Name" },
    { "mData": "ExpType" },
    { "mData": "Date" },
    { "mData": "Term" },
    { "mData": "Exposure" }
    ],
    'aoColumnDefs': [
    { "sTitle": "Pf Id", "aTargets": [0] },
    { "sTitle": "Name", "aTargets": [1] },
    { "sTitle": "Exp Type", "aTargets": [2] },
    { "sTitle": "Date", "aTargets": [3] },
    { "sTitle": "Term", "aTargets": [4] },
    { "sTitle": "Exposure", "aTargets": [5] },
    ]
    });
    [/code]

    The DataTable DOES NOT render when I add records as follows:

    [code]
    JsonTest =
    {
    "PfId": "1000",
    "Name": "Bob Jr.",
    "ExpType": "Hs Var",
    "Date": "05/01/1967",
    "Term": "term 123",
    "Exposure": "55,000,000"
    };
    JsonTest +=
    {
    "PfId": "11000",
    "Name": "James",
    "ExpType": "Hs Var",
    "Date": "06/14/2003",
    "Term": "term 123",
    "Exposure": "56,000,000"
    };
    [/code]

    Can someone help me to properly format my Json object so I can ADD records to it, and subsequently render my DataTable ?

    thanks.
    Bob
  • allanallan Posts: 63,381Questions: 1Answers: 10,449 Site admin
    That's not how Javascript objects work. You want an array of objects like this:

    [code]
    JsonTest = [
    {
    "PfId": "1000",
    "Name": "Bob Jr.",
    "ExpType": "Hs Var",
    "Date": "05/01/1967",
    "Term": "term 123",
    "Exposure": "55,000,000"
    },
    {
    "PfId": "11000",
    "Name": "James",
    "ExpType": "Hs Var",
    "Date": "06/14/2003",
    "Term": "term 123",
    "Exposure": "56,000,000"
    }
    ];
    [/code]

    And drop the array brackets from your aaData value.

    Allan
This discussion has been closed.