Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTables with JSON, AJAX and PHP not displaying any data

I've been trying to get DataTables to work with my existing Ajax search function - which works by itself.

I have the following code:

        $('#SearchResults').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "bRetrieve": true,
            "sAjaxSource": "process.php?action=searchArtifact",
            "fnServerData": function (sSource, aoData, fnCallback){
                aoData.push({
                    "name": "searchName",
                    "value": $('#ArtifactSearch').attr('value')
                });
                $.ajax({
                    "dataType": "json", 
                    "type": "POST", 
                    "url": sSource, 
                    "data": aoData, 
                    "success": fnCallback
                });

            }
        });

The PHP is returning a valid JSON object (using JSON_FORCE_OBJECT):

{"0":{"ARTIFACT_ID":"4E2FE3BCE356C","ARTIFACT_NAME":"123","ARTIFACT_TYPE":"UI","ARTIFACT_LABEL":"Test_Int_EAS_123","ARTIFACT_LOCATION":"Int","ARTIFACT_DOMAIN":"ABC","ARTIFACT_AUTHOR":null,"REGISTERED_EMAIL":"[email protected]","REGISTERED_DATE":"27-07-2011","REGISTERED_TIME":"11:09:00"}

I can see this all fine in FireBug, but my empty table is not being populated with this data.

Any ideas?

@Kyle: Errr - thats it. I guess I don't have one? This is my first attempt (struggle) with DataTables and I'm just copying from the documentation: http://www.datatables.net/usage/callbacks#fnServerData

@MarcB: Added that - but still no data displayed. Thanks for the help

like image 416
Ryan Avatar asked Aug 11 '11 17:08

Ryan


1 Answers

I was having a similar problem. Turns out I wasn't forming the JSON response properly. This worked for me:

<?php

$arr = array ('aaData' => array(
array('3','35','4', '$14,500', '$15,200','$16,900','5','1'),
array('1','16','4', '$14,200', '$15,100','$14,900','Running','1'),
array('5','25','4', '$14,500', '$15,600','$16,900','Not Running','1')
)
);

echo json_encode($arr);
?>
like image 148
BDS1400 Avatar answered Nov 11 '22 19:11

BDS1400