I tried to implement ajax datatables pagination in my code and it is working fine when CSRF in the config is FALSE but when it is TRUE it is not working properly when I check in the inspect element->network the ajax response is showing as "The action you requested is not allowed" please give me a solution without changing the config file.

Here is my controller code:

    function ums_permission_datatable_ajax(){   
        // Datatables Variables
        $draw = intval($this->input->post("draw"));
        $start = intval($this->input->post("start"));
        $length = intval($this->input->post("length"));
        $search = $this->input->post("search");
        $order = $this->input->post("order");

        $col = 0;
        $dir = "";
        if(!empty($order)) {
            foreach($order as $o) {
                $col = $o['column'];
                $dir= $o['dir'];
            }
        }
        if($dir != "asc" && $dir != "desc") {
            $dir = "asc";
        }
        $columns_valid = array(
            "ums_permissions.name", 
            "ums_permissions.function", 
            "ums_permissions.display", 
            "ums_permissions.description", 
            "ums_permissions.system",
        );
        if(!isset($columns_valid[$col])) {
            $order = null;
        } else {
            $order = $columns_valid[$col];
        }
        $orderby[$order] = $dir;

        $total_records = $this->Ums_permissions_model->count();
        //echo '<pre>';print_r($orderby);die;
        $ums_permissions = $this->Ums_permissions_model->retrieve(array(), $length, $start, $orderby);
        //echo $this->db->last_query();die;
        $data = array();
        foreach($ums_permissions as $row) {
            $data[] = array(
                $row->name,                   
                $row->function,             
                $row->display,
                $row->description,
                $row->system,  
                "<a data-toggle='tooltip' data-placement='top' title='Edit' href='".site_url('ums_permissions/add_edit/').$row->id."' class='btn btn-white btn-sm'><i class='fa fa-pencil-square-o fa-fw'></i></a>
                <a data-toggle='tooltip' href='#'  title='Delete' class='btn btn-white btn-sm' role='button' id='delete_wpa_death1' data-deleteid='$row->id'><i class='fa fa-trash fa-fw'></i></a>",
            );
        }
        $output = array("draw" => $draw,"recordsTotal" => $total_records,"recordsFiltered" => $total_records,"data" => $data);  
        echo json_encode($output);
        exit();
    }

Here is my ajax code:

$(document).ready(function() {
    $.fn.dataTable.moment('DD/MM/YYYY');  
                $('.dataTables').dataTable({
                iDisplayLength: 50,
                "initComplete":function(){onint();},
                "processing": true,
                "serverSide": true,
                "ajax": {
                    url : "<?php echo site_url("ums_permissions/ums_permission_datatable_ajax") ?>",
                    type : 'POST',
                    "data": function(d) {
                       var frm_data = $('form').serializeArray();
                       $.each(frm_data, function(key, val) {
                         d[val.name] = val.value;
                       });
                       var searchValue = d.search.value;
                       return d;
                     }
                },
            }); 
 });

Here is my view code:

<table class="table table-striped table-bordered table-hover dataTables" >
                        <thead>
                        <tr>
                            <th><?php echo $field_labels['name']?></th>
                            <th><?php echo $field_labels['function']?></th>
                            <th><?php echo $field_labels['display']?></th>
                            <th><?php echo $field_labels['description']?></th>
                            <th><?php echo $field_labels['system']?></th>
                            <th>Actions</th>
                        </tr>
                        </thead>
                        <tbody>

                        </tbody>
                        <tfoot>
                        <tr>
                            <th><?php echo $field_labels['name']?></th>
                            <th><?php echo $field_labels['function']?></th>
                            <th><?php echo $field_labels['display']?></th>
                            <th><?php echo $field_labels['description']?></th>
                            <th><?php echo $field_labels['system']?></th>
                            <th>Actions</th>
                        </tr>
                        </tfoot>
</table>

0 Answers