japs
japs

Reputation: 11

AJAX JSON to Datatables

I'm still learning this but I'm getting an just [Object object] please help. I've tried to follow the documentation in Datatables but still cant get it right. Hope you can help me... thanks

Here's the code

Javascript

 $(document).ready(function() {
        $.ajax({
            url: "includes/view_ajax.php",
            type: "POST",
            cache: false,
            dataType: 'json',
            success: function(dataResult){
                alert( JSON.stringify(dataResult) );
                $('#memberdata').DataTable({
                    "searching": true,
                    "aaData": [dataResult],
                    "aoColumns": [
                      { "sTitle": "PlateNo" },
                      { "sTitle": "Make" },
                      { "sTitle": "Series" }
                    ] 
                });
            }
        });

PHP

<?php

$columns = array( 
// datatable column index  => database column name
    0 => 'PlateNo',
    1 => 'Make',
    2 => 'Series',
);
include 'database.php';
$sql = "SELECT * FROM vehicleinfo";
$res = mysqli_query($conn, $sql) or die("Error: ".mysqli_error($conn));
$dataArray = array();
while( $row = mysqli_fetch_array($res) ) {


                 $PlateNo = $row["PlateNo"];
                 $Make = $row["Make"];
                 $Series = $row["Series"];
                 
                $dataArray [] =  array("PlateNo" => $PlateNo,
                                        "Make" => $Make,
                                        "Series" => $Series);


}

echo json_encode($dataArray);

?>

Upvotes: 0

Views: 36

Answers (1)

Mohammed Azar
Mohammed Azar

Reputation: 101

$('#memberdata').DataTable({
                    "searching": true,
                    "aaData": [dataResult],
                    "aoColumns": [
                      { "sTitle": "PlateNo" , mData:'PlateNo' },
                      { "sTitle": "Make", mData:'Make' },
                      { "sTitle": "Series", mData:'Series' }
                    ] 
                });

You Need to add mData Property to object of the Column arrays

Upvotes: 1

Related Questions