如何解决ajax数据表分页中的CSRF错误?

时间:2019-04-29 11:09:23

标签: jquery ajax codeigniter datatables csrf

我试图在我的代码中实现ajax数据表分页,当配置中的CSRF为FALSE时,它工作正常,但是当它为TRUE时,当我在检查元素中检查时,它就无法正常工作->网络,显示了ajax响应由于“不允许您执行请求的操作”,请在不更改配置文件的情况下为我提供解决方案。

这是我的控制器代码:

    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();
    }

这是我的Ajax代码:

$(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;
                     }
                },
            }); 
 });

这是我的查看代码:

<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 个答案:

没有答案