Reputation: 929
i'm developing an application with Laravel 5 with Jquery ajax.I have a tab panel in the view.And it's loading an initial form when click a tab button.It loads with jquery ejax without any issue.After that user can enter data to the form and when clicks update button, data should save to the database using jquery ajax.Basically without page refreshing.
Although previous ajax form loading work,when clicks this button,page refreshed and showing a token mismatch exception.I have included csrf tokens as well.Can't figure what's the reason.I have included the csrf token as a meta content value like this,
<meta name="csrf-token" content="{{csrf_token()}}" />
And in jquery ajax code, getting value like this.
var CSRF_TOKEN=$('meta[name="csrf-token"]').attr('content');
Following is the Laravel stack trace,
TokenMismatchException in compiled.php line 2440:
in compiled.php line 2440
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 12083
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 10785
at StartSession->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 11789
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 11738
at EncryptCookies->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in compiled.php line 2478
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in compiled.php line 8944
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in compiled.php line 8935
at Pipeline->then(object(Closure)) in compiled.php line 1891
at Kernel->sendRequestThroughRouter(object(Request)) in compiled.php line 1880
at Kernel->handle(object(Request)) in index.php line 53
Upvotes: 0
Views: 1890
Reputation: 241
php artisan clear-compiled
embed the csrf_token with the request by accessing {{csrf_token()}} in the ajax request.
Upvotes: 0
Reputation: 219
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
In the docs.
https://laravel.com/docs/5.2/routing#csrf-x-csrf-token
Upvotes: 2