Ajax and Laravel - Use meta tag CSRF TOKEN


Include CSRF Token in meta tag with Laravel aplication and Ajax setup in order to use Ajax with Laravel

In addition to checking for the CSRF token as a POST parameter, the Laravel VerifyCsrfToken middleware will also check for the X-CSRF-TOKEN request header. You could, for example, store the token in a "meta" tag:

<meta name="csrf-token" content="{{ csrf_token() }}">

Once you have created the meta tag, you can instruct a library like jQuery to add the token to all request headers. This provides simple, convenient CSRF protection for your AJAX based applications:

$.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });