Send ajax request and handle the response

Send ajax request and handle the response

RazorphynRazorphyn Posts: 5Questions: 0Answers: 0
edited April 2013 in General
Hi,
at this moment I'm using a separate process to load the records inside the datatables:
[code]
$('#deptable').dataTable({
"sDom": "<<'span6'l><'span6'f>r>t<<'span6'i><'span6'p>>",
"sWrapper": "dataTables_wrapper form-inline",
"bDestroy": true,
"bProcessing": true,
"oLanguage": {"sEmptyTable":"No Departments"},
"aoColumns": [
{ "sTitle": "Id","mDataProp": "id" },
{ "sTitle": "Name","mDataProp": "name" },
{ "sTitle": "Active","mDataProp": "active","bSortable": false },
{ "sTitle": "Public","mDataProp": "public","bSortable": false },
{ "sTitle": "Toogle","mDataProp": "action","bSortable": false,"bSearchable":false },
]
});

var request= $.ajax({
type: 'POST',url: '../php/function.php',data: {act:'retrive_depart',sect:'admin'},dataType : 'json',
success : function (data) {
if(data['response']=='ret'){
$('#deptable').dataTable().fnAddData(data['information']);
}
else if(data['response']=='empty'){
}
else{
alert(data[0]);
}
}
});
request.fail(function(jqXHR, textStatus){alert('Ajax Error: '+ textStatus);});
[/code]
and obviously it's horrible, so I have decided to merge the two thing, but with no success:
[code]
$('#deptable').dataTable({
"bDestroy": true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": '../php/function.php',
"sAjaxDataProp": "data.information",
"fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
aoData.push( { "act": "retrive_depart", "sect": "admin" } );
var request = $.ajax( {
"dataType": 'json',
"type": "POST",
"url": '../php/function.php',
"data": aoData,
"success": fnCallback
});
},
"sDom": "<<'span6'l><'span6'f>r>t<<'span6'i><'span6'p>>",
"sWrapper": "dataTables_wrapper form-inline",
"oLanguage": {"sEmptyTable":"No Departments"},
"aoColumns": [
{ "sTitle": "Id","mDataProp": "id" },
{ "sTitle": "Name","mDataProp": "name" },
{ "sTitle": "Active","mDataProp": "active","bSortable": false },
{ "sTitle": "Public","mDataProp": "public","bSortable": false },
{ "sTitle": "Toogle","mDataProp": "action","bSortable": false,"bSearchable":false },
]
});
[/code]
I'm aware that the code is not correct, manly because I can't handle the response (I haven't understood the api very well), but in particular the console says :
Error: TypeError: aData is undefined
Source: http://localhost/DataTables/js/jquery.dataTables.js

The php code:
[code]
else if(isset($_POST['act']) && isset($_POST['name']) && $_POST['act']=='retrive_depart'){
file_put_contents('ef','');
$mysqli = new mysqli($Hostname, $Username, $Password, $DatabaseName);
$stmt = $mysqli->stmt_init();
if($stmt){
$query = "SELECT * FROM ".$SupportDepaTable;
$prepared = $stmt->prepare($query);
if($prepared){
if($stmt->execute()){
$stmt->store_result();
$result = $stmt->bind_result($id, $dname, $active, $public);
if($stmt->num_rows>0){
$departments=array('response'=>'ret','information'=>array());
if($_POST['sect']=='new'){
while (mysqli_stmt_fetch($stmt)) {
if((int)$active==1 && (int)$public==1)
$departments['information'][]="".$dname."";
}
}
else if($_POST['sect']=='admin'){
while (mysqli_stmt_fetch($stmt)) {
$active=((int)$active==0) ? 'No':'Yes';
$public=((int)$public==0) ? 'No':'Yes';
$departments['information'][]=array('id'=>$id,'name'=>$dname,'active'=>$active,'public'=>$public,"action"=>'');
}
file_put_contents('er.txt',print_r($departments,true));
}
echo json_encode($departments);
}
else
echo json_encode(array('response'=>array('empty'),1=>'No Departments Exist'));
}
else
echo json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo json_encode(array(0=>mysqli_stmt_error($stmt)));
}
else
echo json_encode(array(0=>mysqli_stmt_error($stmt)));
$mysqli->close();
}
[/code]
Demo link: http://razorphyn.com/soup/user/admin.php
user:ukkikku@alice.it
pass: admin
after login go to "Administration" section

Is there that can kindly help me?
Thanks a lot
This discussion has been closed.