Invalid JSON response only server side
Invalid JSON response only server side
Hi,
my datatable stoped working few days ago on server. JSON response is empty... but it still works when I runit on locally with MAMP. I can find why.... please help
JS code:
var dataTable = $('#partneriTable').DataTable({
"fnCreatedRow": function (row, data, index) { var info = dataTable.page.info(); var value = index+1+info.start; $('td', row).eq(0).html(value); },
"dom": '<lrtip>',
"language": {"url": "//cdn.datatables.net/plug-ins/1.10.19/i18n/Croatian.json", "loadingRecords": ' ', "processing": '
'},
"lengthChange": true,
"serverSide":true,
"order":[[ 1, "asc" ]],
"ajax":{
data: {action: 'partneri'},
url:"ajax_partneri.php",
type:"POST"
},
"columnDefs":[
{ "targets": [0], "data": null,"sortable": false, "className": 'text-center' },
{ "targets": [1], "searchable": true, "orderable": true, "width": "23%"},
{ "targets": [2], "orderable": false, "searchable": false },
{ "targets": [3,4,5,6], "orderable": false, "searchable": false },
{ "targets": [7,8], "orderable": false, "searchable": true, "className": 'text-center', "width": "4%" },
{ "targets": [9,10], "orderable": false, "searchable": false, "className": 'text-center', "width": "7%" }
]
});
Ajax code:
$partneri = new Partneri();
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action) {
case 'partneri': $partneri->lista();
break;
case 'partneriSingle': $partneri->single();
break;
case 'partneriStorno': $partneri->storno();
break;
}
}
PHP code:
public
function lista() {
## Read value
$draw = $_POST['draw'];
$row = $_POST['start'];
$rowperpage = $_POST['length']; // Rows display per page
$columnIndex = $_POST['order'][0]['column']; // Column index
$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue = $_POST['search']['value']; // Search value
$radioValue = '(kupac = 1 or dobavljac = 1)';
## Radio button value
if(!empty( $_POST[ 'columns' ][ 0 ][ 'search' ][ 'value' ])){
if( $_POST[ 'columns' ][ 0 ][ 'search' ][ 'value' ] == "svi"){
$radioValue = '(kupac = 1 or dobavljac = 1)';
} else {
$radioValue = "(".$_POST[ 'columns' ][ 0 ][ 'search' ][ 'value' ]." = 1)";
}
}
## Search string
if($searchValue != ''){
$searchQuery = " AND (partneri_table.ime LIKE '%".$searchValue."%') ";
}
## Total number of records without filtering
$query = $this->_db->query("SELECT COUNT(*) AS allcount FROM partneri_table WHERE partneri_table.storno = 0");
$totalRecords = $this->_db->first()->allcount;
## Total number of records with filtering
$query = $this->_db->query("SELECT COUNT(*) AS allcount FROM partneri_table WHERE partneri_table.storno = 0 AND ".$radioValue." ".$searchQuery);
$totalRecordwithFilter = $this->_db->first()->allcount;
## Fetch records
$query = $this->_db->query("SELECT
partneri_table.ime,
partneri_table.adresa,
partneri_table.dobavljac,
partneri_table.email,
partneri_table.grad,
partneri_table.gsm,
partneri_table.iban,
partneri_table.id,
partneri_table.kontaktOsoba,
partneri_table.kupac,
partneri_table.oib,
partneri_table.pdvID,
partneri_table.postanskiBroj,
partneri_table.rabat,
partneri_table.skype,
partneri_table.tblDrzave_id,
partneri_table.tblPorezniSustav_id,
partneri_table.telefon,
partneri_table.web
FROM partneri_table
WHERE partneri_table.storno = 0
AND ".$radioValue." ".$searchQuery."
ORDER BY ".$columnName." ".$columnSortOrder."
LIMIT ".$row.", ".$rowperpage.";");
## Prepare results for datatables
$result = $this->_db->results();
$data = array();
foreach ( $result as $row ) {
$kupac;
$dobavljac;
$rabat = $row->rabat . '%';
if ( $row->kupac == 1 ) {
$kupac = '√';
} else {
$kupac = '-';
}
if ( $row->dobavljac == 1 ) {
$dobavljac = '√';
} else {
$dobavljac = '-';
}
$sub_array = array();
$sub_array[] = "row number with jQuery!";
$sub_array[] = $row->ime;
$sub_array[] = $row->adresa;
$sub_array[] = $row->grad;
$sub_array[] = $row->telefon;
$sub_array[] = $rabat;
$sub_array[] = $row->email;
$sub_array[] = $kupac;
$sub_array[] = $dobavljac;
$sub_array[] = '<button type="button" name="update" id="' . $row->id . '" class="btn btn-success dataTableButton btn-xs update" style="width:100px">Uredi</button>';
$sub_array[] = '<button type="button" name="delete" id="' . $row->id . '" class="btn btn-danger dataTableButton btn-xs delete" style="width:100px">Izbriši</button>';
$data[] = $sub_array;
}
$output = array(
"draw" => intval($draw),
"recordsTotal" => $totalRecords,
"recordsFiltered" => $totalRecordwithFilter,
"data" => $data
);
echo json_encode( $output );
}
When I use print_r($output) I get:
Array
(
[draw] => 1
[recordsTotal] => 47
[recordsFiltered] => 47
[data] => Array
(
[0] => Array
(
[0] => row number with jQuery!
[1] => 4H break d.o.o.
[2] => Franje Pu�kari?a 1d
[3] => Lu?ko
[4] => +38516531008
[5] => 0%
[6] => info@4h-break.hr
[7] => √
[8] => -
[9] => <button type="button" name="update" id="32" class="btn btn-success dataTableButton btn-xs update" style="width:100px">Uredi</button>
[10] => <button type="button" name="delete" id="32" class="btn btn-danger dataTableButton btn-xs delete" style="width:100px">Izbriši</button>
)
[1] => Array
(
[0] => row number with jQuery!
[1] => ABL SURSUM Bayerische Elektrozubeh�r GmbH & Co. KG
[2] => Albert-B�ttner-Str. 11
[3] => Lauf / Pegnitz
[4] => +4991231880
[5] => 0%
[6] => info@abl.de
[7] => -
[8] => √
[9] => <button type="button" name="update" id="20" class="btn btn-success dataTableButton btn-xs update" style="width:100px">Uredi</button>
[10] => <button type="button" name="delete" id="20" class="btn btn-danger dataTableButton btn-xs delete" style="width:100px">Izbriši</button>
)
[2] => Array
(
[0] => row number with jQuery!
[1] => ASK d.o.o.
[2] => Usko?ka 33
[3] => Zagreb
[4] => +38513028149
[5] => 0%
[6] => info@ask.com.hr
[7] => √
[8] => √
[9] => <button type="button" name="update" id="12" class="btn btn-success dataTableButton btn-xs update" style="width:100px">Uredi</button>
[10] => <button type="button" name="delete" id="12" class="btn btn-danger dataTableButton btn-xs delete" style="width:100px">Izbriši</button>
)
[3] => Array
(
[0] => row number with jQuery!
[1] => Bamex
[2] => Krapinska 10
[3] => Zlatar Bistrica
[4] => 049 461 995
[5] => 0%
[6] => bamex.jakusic@gmail.com
[7] => √
[8] => √
[9] => <button type="button" name="update" id="24" class="btn btn-success dataTableButton btn-xs update" style="width:100px">Uredi</button>
[10] => <button type="button" name="delete" id="24" class="btn btn-danger dataTableButton btn-xs delete" style="width:100px">Izbriši</button>
)
and so on.....
)
Answers
First thing to do is check your server's error logs. If it is sending an empty response, you can't debug that on its own since there are no error messages!
You could also add:
At the top of your PHP file to get it to display the errors.
Allan
Thanks for quick response! I'll put the lines in the code and see what the errors are.
I have put
error_reporting(E_ALL);
ini_set('display_errors', '1');
and response is still empty.
Did you have a look at the server's error logs?
If it works locally and not remotely, then the remote server is configured differently and it is a case of working out what that difference is and how to make it work on the remote server.
Allan