Server side (PHP):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
protected function json_response($code=200, $message='', $data=null){ http_response_code($code); //Output; header('Cache-Control: no-cache, must-revalidate'); header('Content-type: application/json;charset=utf-8'); $out = array( 'code' => $code, 'message' => $message, 'data' => $data ); echo json_encode($out); exit(); } |
Client (with datatable):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
function init_dt(){ datatable = $t.DataTable({ "searching": true, "info": true, "lengthChange": true, "paging": true, 'serverSide': true, 'processing': true, "ajax": { 'url': base_url + 'admin_ajax/products/all', 'data': function(d){ var $c = $('.option_panel'); d.store_id = $('#store').val(); d.campaign_id = $('#campaign').val(); d.cat_id = $('#cat').val(); }, error: function (xhr, error, thrown) { //error( xhr, error, thrown ); console.log(xhr); console.log(error); console.log(thrown); var response = xhr.responseJSON; var msg = response.message; alert(msg); } }, "order": [[ 1, "desc" ], [ 0, "asc" ]], "columnDefs": [ { "targets": [ 0 ], "visible": true, "searchable": false } ], "drawCallback": function( settings ) { var api = this.api(); //wrap.loaded(settings); loaded(settings); }, 'initComplete': function(settings, json){ //loaded(settings); }, "language" : { "url": "/assets/plugins/datatable_zhtw.json" } }); } |
Result in Chrome dev tool: