JSON data from server failed could not be parsed -- JSR 286 portlet
JSON data from server failed could not be parsed -- JSR 286 portlet
myahmed29
Posts: 3Questions: 0Answers: 0
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
[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
This discussion has been closed.
Replies
Allan
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!
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
[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