javascript - How to get AJAX response before sending to template using DATATABLES? -
i implementing log tables in admin panel , used datatable plugin. created ajax in datatable don't know how response before sending table.
i have in jquery:
<script type="text/javscript"> $(document).ready(function() { $('#log-pageview') .on('xhr .dt', function(e, settings, json) { $('#status').html(json.status) }) .datatable({ "processing": true, "serverside": true, "ajax": "/secure/logs/visits.php?log_get=1" }); }); </script>
in html have this:
<table id="log-pageview" class="display" cellspacing="0" width="100%"> <thead> <tr> <th>log id</th> <th>user id</th> <th>ip address</th> <th>date viewed</th> </tr> </thead> <tfoot> <tr> <th>log id</th> <th>user id</th> <th>ip address</th> <th>date viewed</th> </tr> </tfoot> </table> <div id="status"></div>
in server side php have this.
function get_pageview() { $host = mysql_connect('localhost','avjunky_2','1qaz2wsx') or die(mysql_error()); mysql_set_charset('utf8', $host); //added character encoding, made global functions /** added rochelle **/ $db = mysql_select_db('avjunky_2',$host) or die(mysql_error()); $log_array = array(); $log_data = array(); $get_all_logs = mysql_query("select id, logged_id, ip_address, date_viewed avjunky_pageview", $host); while($row_logs = mysql_fetch_array($get_all_logs)) { $log_data[] = array( 'id' => $row_logs['id'], 'logged_id' => $row_logs['logged_id'], 'ip_address' => $row_logs['ip_address'], 'date_viewed' => $row_logs['date_viewed'] ); } $total_count = count($log_data); $log_array = array( 'draw' => 1, 'recordstotal' => $total_count, 'recordsfiltered' => $total_count, 'data' => $log_data ); echo json_encode($log_array); } if(isset($_get['log_get'])) { get_pageview(); }
in server side generated kind of json:
{"draw":1,"recordstotal":2,"recordsfiltered":2,"data":[{"id":"3","logged_id":"7","ip_address":"122.2.55.11","date_viewed":"2015-03-16 10:10:42"},{"id":"2","logged_id":"8","ip_address":"122.2.55.11","date_viewed":"2015-03-17 00:05:40"}]}
{ "draw":1, "recordstotal":2, "recordsfiltered":2, "data":[ { "id":"3", "logged_id":"7", "ip_address":"122.2.55.11", "date_viewed":"2015-03-16 10:10:42" }, { "id":"2", "logged_id":"8", "ip_address":"122.2.55.11", "date_viewed":"2015-03-17 00:05:40" } ] }
i don't know did go wrong. checked developer tool network tab , seems ajax not calling url.
in ajax tried call response url this.
http://mysite/admin/secure/logs/visits.php?log_get=1
but doesn't load data well.
firstly, use xhr.dt
datatables > 1.10 should used.
secondly, data should of following structure.
{ "draw":1, "recordstotal":2, "recordsfiltered":2, "data":[ [ "3", "7", "122.2.55.11", "2015-03-16 10:10:42" ], [ "2", "8", "122.2.55.11", "2015-03-17 00:05:40" ] ] }
here demo
Comments
Post a Comment