Valid JSON but no table data is shown.

Valid JSON but no table data is shown.

jboykinsjboykins Posts: 2Questions: 0Answers: 0
edited September 2012 in General
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.

Replies

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    edited September 2012
    You've got a trailing comma after aoColumns - so IE is going to reject that. However, beyond that, I think we'd need to we'd need to see either a demo page or at least the JSON data being used.

    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
  • jboykinsjboykins Posts: 2Questions: 0Answers: 0
    Thanks 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.
This discussion has been closed.