JSON data via PHP and Ajax

JSON data via PHP and Ajax

mrbladergroenmrbladergroen Posts: 2Questions: 1Answers: 0

Hi there,

I am fairly new to Datatables. I have been searching for the answer, but I did not find one. I have the following problem:
When I read JSON data from a file, I get the information I need on screen. I use the following code:

$('#sampleTable').DataTable({
    'ajax':'../" .$_POST['database'] ."/test.json',
    'columns' : [
        { 'data' : 'SampleBarcode' }
    ]
});

and my JSON file looks like:

{"data":[
    {
        "SampleBarcode":"BlaBLa"
    }
]
}

When I read the same information from a query using php and return the data in JSON format I get "No matching records found", while I do get the JSON data. The code I use in this case looks like this:

$('#sampleTable').DataTable({
     'serverSide': true,
     'ajax':{
            'type': 'POST',
            'url': '../samplemanagement/samplemanagement_base.php',
            'data': sendarray,
            'dataSrc': function(responseText) {
                 return responseText;
            }
      },
      'columns' : [
            { 'data' : 'SampleBarcode' }
     ]
});

samplemanagement_base.php is quite elaborate, but the most important code looks like:

$qAllSamples = "SELECT TOP 3 * FROM CPM_SampleInfo2 WHERE Department= ? AND SampleBarcode LIKE ?";
$records = array();
try {
        ... (do query and get the results)
        $records["data"][] = $record;
} catch (PDOException $exception) {
    echo $exception->getMessage();
    exit;
}
if(sizeof($records)==0)
{
    $record = new stdClass();
    $record->SampleBarcode = "BlaBla";
        $records["data"][] = $record;
}
echo json_encode($records);

And the JSON line I get back looks like:

{"data":[{"SampleBarcode":"BlaBla"}]}

which in principle is the same as above. The only difference is that this is on a single line and not over multiple lines.

What am I not seeing here? The JSON data look the same, but in the first case I do get the data on screen and in the second case not. Can anyone help me out?

Cheers, Marco

Answers

  • kthorngrenkthorngren Posts: 21,351Questions: 26Answers: 4,955

    Are you wanting to implement Server Side Processing (serverSide: true)? Its a paging protocol for large data sets and your server script needs to support the protocol defined in the link. Please see this FAQ to help determine if you need server side processing. If not then comment out the line to see what happens. If it works as expected then remove the line.

    Based on what you posted has your JSON response I suspect that you won't need the ajax.dataSrc option. Datatables will default to looking in the data object for an array of row data which is what you have.

    Kevin

  • mrbladergroenmrbladergroen Posts: 2Questions: 1Answers: 0

    Hi Kevin,

    Thanks for the answer. I don't think however that the dataSrc option is the problem. The example is with only three records, but in the future I want to extend the result to many records. That is why I included the serverSide processing. I did find however that the dataSrc option needs the name of the array, which defaults to 'data'. I don't need to return the whole array (or json object). When I just say dataSrc:'data', or in my case dataSrc:'samples' then it works. Problem solved. Thanks again for your help.

    Marco

This discussion has been closed.