Valid JSON but no table data is shown.
Valid JSON but no table data is shown.
Hello everyone,
I'm having trouble getting my JSON array to properly show data inside a datatable. I've verified that the JSON is correct by using JSONLint. Although the issue still persists. My case is as described below:
1. The JSON data is returned by PHP using json_encode.
2. Though the JSON formatting is valid, no data is rendered.
3. The debugging report is here: http://debug.datatables.net/udiden
Interestingly, when I play with how the data is formatted before being sent by PHP, I can get the table to output a single row correctly. Though when using the debugger on this working page it still says that it was a JSON parsing error. I've included the link to that report here: http://debug.datatables.net/enasem
Here is the jQuery I used:
[code]
$.ajax({
type: "POST",
url: '../system/intercept.php',
datatype: 'JSON',
data: {
"tag": tag,
"userId": userId,
"authToken": authToken,
"authHash": authHash,
"loadObj": loadObj,
"loadObjId": loadObjId
},
success: function (results) {
var results = eval('('+results+')');
val = 0;
$('.siteName').html(results.siteName);
$('.lastUpdated').html(results.lastUpdated);
$('.postsLastDay').html(results.postsLastDay);
$('.postsLastWeek').html(results.postsLastWeek);
$('.postsLastMonth').html(results.postsLastMonth);
$('.postsToDate').html(results.postsToDate);
console.log(JSON.stringify(results.repsOnSite));
$('#example').dataTable({
"bJQueryUI": true,
"bPaginate": false,
"bProcessing": true,
"aaData": [results.repsOnSite],
"aoColumns": [
{ "sTitle": "Rep", "mData": "Rep" },
{ "sTitle": "Total Posts", "mData": "cell1" },
{ "sTitle": "Posts Yeserday", "mData": "cell1" },
{ "sTitle": "Posts Last Week", "mData": "cell1" },
{ "sTitle": "Posts Last Month", "mData": "cell1" },
{ "sTitle": "View Posts", "mData": "cell1" }
],
});
},
error: function (msg) {
$('.details').html('We're sorry, an error has occured. Please try again later. ');
}
});
[/code]
I greatly appreciate any assistance or insight you guys can provide.
Sincerely,
A guy with considerably more hair than when he started.
I'm having trouble getting my JSON array to properly show data inside a datatable. I've verified that the JSON is correct by using JSONLint. Although the issue still persists. My case is as described below:
1. The JSON data is returned by PHP using json_encode.
2. Though the JSON formatting is valid, no data is rendered.
3. The debugging report is here: http://debug.datatables.net/udiden
Interestingly, when I play with how the data is formatted before being sent by PHP, I can get the table to output a single row correctly. Though when using the debugger on this working page it still says that it was a JSON parsing error. I've included the link to that report here: http://debug.datatables.net/enasem
Here is the jQuery I used:
[code]
$.ajax({
type: "POST",
url: '../system/intercept.php',
datatype: 'JSON',
data: {
"tag": tag,
"userId": userId,
"authToken": authToken,
"authHash": authHash,
"loadObj": loadObj,
"loadObjId": loadObjId
},
success: function (results) {
var results = eval('('+results+')');
val = 0;
$('.siteName').html(results.siteName);
$('.lastUpdated').html(results.lastUpdated);
$('.postsLastDay').html(results.postsLastDay);
$('.postsLastWeek').html(results.postsLastWeek);
$('.postsLastMonth').html(results.postsLastMonth);
$('.postsToDate').html(results.postsToDate);
console.log(JSON.stringify(results.repsOnSite));
$('#example').dataTable({
"bJQueryUI": true,
"bPaginate": false,
"bProcessing": true,
"aaData": [results.repsOnSite],
"aoColumns": [
{ "sTitle": "Rep", "mData": "Rep" },
{ "sTitle": "Total Posts", "mData": "cell1" },
{ "sTitle": "Posts Yeserday", "mData": "cell1" },
{ "sTitle": "Posts Last Week", "mData": "cell1" },
{ "sTitle": "Posts Last Month", "mData": "cell1" },
{ "sTitle": "View Posts", "mData": "cell1" }
],
});
},
error: function (msg) {
$('.details').html('We're sorry, an error has occured. Please try again later. ');
}
});
[/code]
I greatly appreciate any assistance or insight you guys can provide.
Sincerely,
A guy with considerably more hair than when he started.
This discussion has been closed.
Replies
btw:
> var results = eval('('+results+')');
Is that really needed? Doesn't jQuery pass in JSON for the json dataType? *edit* - I think it should be dataType, rather than datatype as you have.
Allan
It was actually a combination of things.
1. It should be "dataType" instead of "datatype"
2. I had used the eval function to correct the issue of my JSON not being returned as a JSON array.
3. The extra brackets around "results.repsOnSite" were causing the issue.