laravelUnder the framework, sometimes when requesting using methods such as post or put, a TokenMismatchExceptionin VerifyCsrfToken.php line 67 error will sometimes be reported. The reason is that laravel has anti-CSRF turned on by default.
There are two ways to solve this problem, one is to submit the token value when requesting, and the other is to exclude the requested route when preventing CSRF
Method 1: Pass the token value
When submitting the form:
When using AJAX request:
$.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });
Method 2: Exclude the specified URL from CSRF protection
For example, the accessed URL is http://laravel.com/photo/12, and now you want to exclude routes related to photo resources, then add the route in App\Http\Middleware\VerifyCsrfToken::class as follows:
protected $except = [ 'photo', 'photo/*', ];
Note that method two will not be able to protect photo-related routes from CSRF, so please choose according to the actual situation
The above is the entire content of this article. For more laravel content, please pay attention tolaravel framework introductory tutorial.
Recommended related articles:
In-depth analysis of appearance mode in Laravel framework
vue.js in laravel5.4 framework Implementing Ajax form submission error verification
Related video recommendations:
The latest five Laravel video tutorial recommendations in 2017
The above is the detailed content of Exception handling content of TokenMismatchException in laravel framework. For more information, please follow other related articles on the PHP Chinese website!