Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post Method with CRSF Regenerate true #35

Open
reactmore opened this issue Mar 18, 2023 · 1 comment
Open

Post Method with CRSF Regenerate true #35

reactmore opened this issue Mar 18, 2023 · 1 comment

Comments

@reactmore
Copy link

is it possible to Send a response with a csrf token so that it can be obtained in the ajax process? or is there another way of implementing

     /**
     * --------------------------------------------------------------------------
     * CSRF Regenerate
     * --------------------------------------------------------------------------
     *
     * Regenerate CSRF Token on every submission.
     */
    public bool $regenerate = true;
public function toJson($returnAsObject = NULL)
    {

        if(! Request::get('draw'))
        {
            return self::throwError('no datatable request detected');
        }

        if($returnAsObject !== NULL) 
            $this->columnDefs->returnAsObject($returnAsObject);

        $this->query->setColumnDefs($this->columnDefs);
        
        $response = Services::response();

        $callback = [
            'draw'              => Request::get('draw'),
            'recordsTotal'      => $this->query->countAll(),
            'recordsFiltered'   => $this->query->countFiltered(),
            'data'              => $this->query->getDataResult(),

        ];

        $csrf_name = csrf_token();
        $csrf_hash = csrf_hash();
        $callback[$csrf_name] = $csrf_hash;
        
        return $response->setJSON($callback);
    }
var serversidetable = $('#' + id).DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                "url": url,
                "data": function (data) {
                    data.app_csrf_token = $('input[name=app_csrf_token]').val() //function bridge token view to controller (required)
                },
                "dataSrc": function (response) {
                    $('input[name=app_csrf_token]').val(response.app_csrf_token); //dataSrc for random request token char (required)
                    return response.data;
                },
                "method": 'POST'
            },
            "responsive": true,
            "lengthChange": true,
            "autoWidth": false,
            "aLengthMenu": [[15, 30, 60, 100], [15, 30, 60, 100, "All"]]
        });
@zulyantara
Copy link

sebenernya csrf true/false, returnnya tetap menampilkan csrf gak ada masalah, bagusnya di variabel $callback langsung tambahkan
'csrf_token => csrf_hash(),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants