0

I can not get my DataTable to load the datas by a route in Api.php type POST.

I am using this DataTables: https://datatables.net/

Api.php

Route::post('subredes/{username}/{token}', function($username, $token){
    $user = DB::table('usuarios')
                    ->where('token', $token)
                    ->value('username');
    $estado = DB::table('usuarios')
                    ->where('username', $username)
                    ->value('estado');  

    if(!empty(trim($user)) && $estado == 2){
        $query = DB::table('subredes as s')
            ->select('s.id', 's.ip', 's.gateway', 's.mask');

        return datatables()
            ->of($query)
            ->addColumn('btn','Actions.subredes')
            ->rawColumns(['btn'])
            ->toJson();
    }

});

Javascript

    $(document).ready(function(){
        $('#Subredes').DataTable({
        "bAutoWidth": false,
        "language":{
            "url": "{{url('api/spanish')}}"
        },
        "destroy": true,
        "responsive": true,
        "serverSide":true,
        "ajax": {
            "url": "{{url('api/subredes/'.auth()->user()->username.'/'.auth()->user()->token)}}",
            "type": "POST",
        },
        "columnDefs": [{
        "targets": 'no-sort',
        "orderable": false,
        "searchable": false,
        }],
        "columns":[
            {data: 'ip', name: 's.ip'},
            {data: 'gateway', name: 's.gateway'},
            {data: 'mask', name: 's.mask'},
            {data: 'btn'},
        ]
        });
    });

Error:

enter image description here

I suspect that it is because he did not give him the CSRF, but I do not know how to do it and I am not 100% sure that this is the problem.

1 Answer 1

1

The problem is that DataTables is making a GET request and you only allow POST. The error is in the ajax configuration: You have to replace type with method and it will send it as POST.

Source: https://api.jquery.com/jQuery.ajax/

The CSRF token will be your next problem. The simplest method would be to use GET instead of post (in the DataTables configuration and the Route configuration). Otherwise you will have to expose the token somewhere in your HTML (e.g. a meta tag) and send it with the request:

    let token = document.head.querySelector('meta[name="csrf-token"]');
    //...
        "ajax": {
            "url": "{{url('api/subredes/'.auth()->user()->username.'/'.auth()->user()->token)}}",
            "method": "POST",
            "data": {
                _token: token,
        },
Sign up to request clarification or add additional context in comments.

2 Comments

Right now I get a problem in the jquery that says: Uncaught TypeError: Illegal invocation
I think it was at the time of assigning the value to the variable token use $ ('meta [name = csrf_token]'). attr ("content") and it works correctly, will there be any problem doing it this way?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.