Laravel //AUTO SET CSRF TOKEN REQUEST AJAX

<!-- put this 'meta' between tag head in begining html --> <meta name="csrf-token" content="{{ csrf_token() }}"> <!-- put this in your main javascript file. eg.: "main.js" --> <script> // With code bellow all ajax request will send 'X-CSRF-TOKEN' by default. // Now, you don't need more make like this to take token: // $.post('http://localhost:8000/clientes', // {id: 3, _token: $('meta[name="csrf-token"]'} // take csrf-token // ); $.ajaxPrefilter(function( options ) { // if your request dont CrossDomain (via other site) add the token if ( ! options.crossDomain ) { // Header CSRF TOKEN options.headers = { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }; // remove the headers } else { options.headers = null; } }); </script> <!-- // Sorry about that... if ( You.likeIt() ) return 'http://richellyitalo.com && http://twitter.com/richellyitalo'; -->
With ajax request, the laravel need check token (csrf).
All time you need add to this request, a token. Now, no more!
The 'ajaxPrefilter' will add the code automatically to your request.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.