I got 403 forbidden when I post second query in Ajax. (Ajax + codeigniter 3 + datatable)

Viewed 154

I'm using Codeginiter3 and i tried datatable-ajax. I realized that this error is from CSRF protection and I sent my CSRF name and hash codes to my ajax post. I solved the problem but I am getting the same error again when searching the datatable or going to other page.

My Controller:

public function tempList(){

        $columns = array(
            0 =>'id',
            1 =>'message',

        );

        $limit = $this->input->post('length');
        $start = $this->input->post('start');
        $order = $columns[$this->input->post('order')[0]['column']];
        $dir = $this->input->post('order')[0]['dir'];

        $totalData = $this->TemplateModel->allposts_count();

        $totalFiltered = $totalData;

        if(empty($this->input->post('search')['value']))
        {
            $posts = $this->TemplateModel->allposts($limit,$start,$order,$dir);
        }
        else {
            $search = $this->input->post('search')['value'];

            $posts =  $this->TemplateModel->posts_search($limit,$start,$search,$order,$dir);

            $totalFiltered = $this->TemplateModel->posts_search_count($search);
        }

        $data = array();
        if(!empty($posts))
        {
            foreach ($posts as $post)
            {

                $nestedData['id'] = $post->id;
                $nestedData['message'] = $post->message;
                $data[] = $nestedData;

            }
        }

        $json_data = array(
            "draw"            => intval($this->input->post('draw')),
            "recordsTotal"    => intval($totalData),
            "recordsFiltered" => intval($totalFiltered),
            "data"            => $data
        );

        echo json_encode($json_data);


    }

My Model :

 function allposts_count()
    {
        $query = $this
            ->db
            ->get($this->table);

        return $query->num_rows();

    }

    function allposts($limit,$start,$col,$dir)
    {
        $query = $this
            ->db
            ->limit($limit,$start)
            ->order_by($col,$dir)
            ->get($this->table);

        if($query->num_rows()>0)
        {
            return $query->result();
        }
        else
        {
            return null;
        }

    }

    function posts_search($limit,$start,$search,$col,$dir)
    {
        $query = $this
            ->db
            ->like('id',$search)
            ->or_like('message',$search)
            ->limit($limit,$start)
            ->order_by($col,$dir)
            ->get($this->table);


        if($query->num_rows()>0)
        {
            return $query->result();
        }
        else
        {
            return null;
        }
    }

    function posts_search_count($search)
    {
        $query = $this
            ->db
            ->like('id',$search)
            ->or_like('message',$search)
            ->get($this->table);

        return $query->num_rows();
    }

My View:

<div class="container container-margin">
    <div class="row">
        <div class="col-sm-12">
            <div class="mb-3 mt-3">
                <h1 class="text-center">Templates</h1>
            </div>
            <div class="card">
                <div class="card-body">
                    <table id="tempTable" class="display" style="width:100%">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>Message</th>
                        </tr>
                        </thead>


                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

My Js:

 $('#tempTable').DataTable({
            'processing': true,
            'serverSide': true,
            'ajax': {
                'url': '<?php echo base_url('Dashboard/tempList') ?>',
                "dataType" : "json",
                "type" : "POST", // ajax source
                "data":{  '<?php echo $csrfName ?>'    : '<?php echo $csrfHash ?>' }



            },

            'columns': [
                {data: 'id'},
                {data: 'message'},

            ]

        });

Error Img : Error Img

1 Answers

You are having the 403 error on the second request because Cross-site request forgery (CSRF) generates a unique value per request.

For solution, goto "application/config/config.php" and change "$config['csrf_protection']" to false. This is stop generating new hash value on each request.

Related