Home> PHP Framework> Laravel> body text

Exception handling content of TokenMismatchException in laravel framework

不言
Release: 2018-07-31 15:27:35
Original
3335 people have browsed it

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:

Copy after login

When using AJAX request:

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

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/*', ];
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!