Trouble with my aaData in ajax post.

Trouble with my aaData in ajax post.

dllabsdllabs Posts: 5Questions: 2Answers: 0

I'm trying to generate a table that has column names that are set in the json reply to an ajax.post. To do so I am using the following javascript:

    $.ajax( {
        "url": "get_table.php",
        "type": "POST",
        "data": {
            "s1": series_1,
            "s2": series_2,
            "s3": series_3,
            "t1": topic_1,
            "q1": question_1
        },
        "success": function ( data ) {
            $(document).ready(function() {
                $('#datatable').dataTable(data);
            } );    
        },
        "dataType": "json"
    } );

I have the column names working perfectly using the aoColumns field in the json data, but the aaData part of my json data seems to produce the well-known error "Requested unknown parameter 0 for row zero".
When I hit OK on that error message, I get the table exactly as I want it, with the number of rows expected, only all the rows are blank.
If I return aoColumns but don't return any aaData at all, I don't get any errors, just a perfectly formed empty table with the correct column names. So it seems to me I'm really close to making this work :)

I can't for the life of me figure out what I am doing wrong. Is there something wrong with the json data my server is returning, below? Obviously I'm a total noob, and could do with any help or advice.

John

{
    "aoColumns": [
        {
            "sTitle": "Firm"
        },
        {
            "sTitle": "ID"
        },
        {
            "sTitle": "Notes"
        },
        {
            "sTitle": "January 2014"
        },
        {
            "sTitle": "July 2014"
        },
        {
            "sTitle": "July 2013"
        }
    ],
    "aaData": [
        {
            "Firm": "Woolworths",
            "ID": "17",
            "Notes": "Notes",
            "Column_1": "161",
            "Column_2": "158",
            "Column_3": "165"
        },
        {
            "Firm": "Coles",
            "ID": "18",
            "Notes": null,
            "Column_1": "37",
            "Column_2": "36",
            "Column_3": "37"
        }
    ]
}

Answers

  • dllabsdllabs Posts: 5Questions: 2Answers: 0

    PS I had iTotalRecords, iTotalDisplayRecords and sEcho in my json, just in case it was looking for server-side formatted data, but that didn't seem to make any difference.

  • dllabsdllabs Posts: 5Questions: 2Answers: 0

    I just put the sample site up:
    http://dllabs.org/surveys/test/

  • dllabsdllabs Posts: 5Questions: 2Answers: 0

    figured it out: I was missing mDataProp for each column in aoColumns.
    Thus the json should read:

     "aoColumns": [
            {
                "sTitle": "Firm",
                "mDataProp": "firm"
            },
            {
                "sTitle": "ID",
                "mDataProp": "id"
            },
            {
                "sTitle": "Notes",
                "mDataProp": "notes"
            },
            {
                "sTitle": "March 2012",
                "mDataProp": "column_1"
            },
            {
                "sTitle": "June 2012",
                "mDataProp": "column_2"
            },
            {
                "sTitle": "May 2013",
                "mDataProp": "column_3"
            }
        ],
    
This discussion has been closed.