1.-si tienes esto: <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tu título aquí</title> <!-- Aquí agregas el token CSRF --> <meta name="csrf-token" content="{{ csrf_token() }}"> <!-- Otros estilos y scripts --> <link rel="stylesheet" href="..."> </head> puedes hacer simplemente esto segun esta es la manera estandar y mas sencilla: $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); 2.- si tienes esto: <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tu título aquí</title> <!-- Aquí agregas el token CSRF --> <meta name="csrf-token" content="{{ csrf_token() }}"> <!-- Otros estilos y scripts --> <link rel="stylesheet" href="..."> </head> puedes hacer dos cosas capturar una variable dentro de una script asi: const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); luego haces esto: $('#usuarios').DataTable({ ajax: { url: '/usuarios/data', // URL al método que devuelve JSON type: 'POST' , data: { _token: token, // CSRF token } //linea relevante es esta }, processing: true, serverSide: true, columns: [ { data: 'persona.nombres' }, { data: 'persona.apellidos' }, { data: 'persona.email' }, { data: 'institutos' }, { data: 'persona.genero.nombre' }, { data: 'fecha_nacimiento2' }, { data: 'acciones' } ], language: { url: "/js/es-ES.json", }, pageLength: 3, }); 3.- @push('scripts') <script> const token = '{{ csrf_token() }}'; </script> @stack('scripts') @endpush luego haces esto: $('#usuarios').DataTable({ ajax: { url: '/usuarios/data', // URL al método que devuelve JSON type: 'POST' , data: { _token: token, // CSRF token } //linea relevante es esta }, processing: true, serverSide: true, columns: [ { data: 'persona.nombres' }, { data: 'persona.apellidos' }, { data: 'persona.email' }, { data: 'institutos' }, { data: 'persona.genero.nombre' }, { data: 'fecha_nacimiento2' }, { data: 'acciones' } ], language: { url: "/js/es-ES.json", }, pageLength: 3, });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter