-1

I write a script code that calls the getdata.php by sending a value ,i am getting the success function response in json format it look like this.

my script code:

<script>
 function getId(val) {
 alert(val);
    $.ajax({
     type: "post",
    url: "getdata.php",
    data: 'id=' + val,
    dataType: "JSON",                  
    success: function(response){ 
      console.log(response);
    }                       
            /*$('#city').dataTable({
             "bProcessing": true,
             "sAjaxSource": "JSON.parse(this.response)",
             "aoColumns": [
                    { mData: 'id' } ,
                    { mData: 'vid' },
                    { mData: 'date' },
                    { mData: 'latitude' },
                    { mData: 'longitude' },
                    { mData: 'speed' },
                    { mData: 'batery' },
                    { mData: 'totalkm' },
                    { mData: 'intrkm' },
                    { mData: 'control' }

            ]
    }); */


 });
}
</script>

and my response json data :

{"sEcho":1,"iTotalRecords":2,"iTotalDisplayRecords":2,"aaData":[{"id":"1","vid":"A0001","date":"2019-02-07 15:58:53","latitude":"17.00","longitude":"82.20","speed":"25","batery":"98","totalkm":"8","intrkm":"8","control":"0"},{"id":"2","vid":"A0001","date":"2019-02-07 15:58:53","latitude":"17.10","longitude":"82.30","speed":"26","batery":"97","totalkm":"15","intrkm":"15","control":"0"}]}

console.log(response); gives like

{sEcho: 1, iTotalRecords: 2, iTotalDisplayRecords: 2, aaData: Array(2)}
aaData: Array(2)
0: {id: "1", vid: "A0001", date: "2019-02-07 15:58:53", latitude: "17.00", 
longitude: "82.20", …}
1: {id: "2", vid: "A0001", date: "2019-02-07 15:58:53", latitude: "17.10", 
longitude: "82.30", …}
length: 2
__proto__: Array(0)
iTotalDisplayRecords: 2
iTotalRecords: 2
sEcho: 1

Now, how to give the response of ajax call to datatables sAjax source and read the colums data and present it in #city. please help me i am strucked here.

1
  • Try out this Commented Feb 18, 2019 at 4:59

1 Answer 1

0

DataTables has the ability to read data from virtually any JSON data source that can be obtained by Ajax. This can be done, in its most simple form, by setting the ajax option to the address of the JSON data source.

$(document).ready(function() {
    $('#city').DataTable( {
        "ajax": 'getdata.php?any_value=1'
    });
});

Refer more details : https://datatables.net/examples/data_sources/ajax

Sign up to request clarification or add additional context in comments.

2 Comments

Your JSON should be like this { "data": [ [ "Tiger Nixon", "System Architect", "Edinburgh", "5421", "2011/04/25", "$320,800" ], [ "Garrett Winters", "Accountant", "Tokyo", "8422", "2011/07/25", "$170,750" ]]
@Srinivas Have you fixed ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.