简体   繁体   English

如何在Laravel 5.2中的VerifyCsrfToken.php 67行中修复TokenMismatchException

[英]how to fix TokenMismatchException in VerifyCsrfToken.php line 67 in laravel 5.2

I am working with Laravel 5.2 and got above error when I try to submit form data. 我正在使用Laravel 5.2,尝试提交表单数据时出现上述错误。

My blade file is this. 我的刀片文件是这个。

 @extends('layouts.app1')

    @section('title')
    <h3>Board of Directors</h3>
    @endsection


    @section('content')


    <script type="text/javascript" src="{{ asset('/js/tinymce/tinymce.min.js') }}"></script>
<script type="text/javascript">
  tinymce.init({
    selector : "textarea",
    plugins : ["advlist autolink lists link image charmap print preview anchor", "searchreplace visualblocks code fullscreen", "insertdatetime media table contextmenu paste"],
    toolbar : "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
  }); 
</script>

    <form action="/create" method="post">
 <div class="form-group">
      <textarea name='body'class="form-control">{{  old('body')  }}</textarea>
      </div>
      <input type="submit" name='publish' class="btn btn-success" value = "Publish"/>
      <input type="submit" name='save' class="btn btn-default" value = "Save Draft" />
    </form>
    @endsection

how can I fix this problem? 我该如何解决这个问题?

您应该在表单标签内添加CSRF字段

{!! csrf_field() !!}

Anytime you define a HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request. 每当您在应用程序中定义HTML表单时,都应在表单中包含一个隐藏的CSRF令牌字段,以便CSRF保护中间件可以验证请求。 You may use the csrf_field helper to generate the token field: 您可以使用csrf_field帮助器来生成令牌字段:

<form method="POST" action="/profile">
    {{ csrf_field() }}
    ...
</form>

Reference 参考

尝试在config/session.php文件中设置'domain' => 'to your domain'

Another posible aproach and mostly if you are building an API with no views and you are receiving this error is to put the route(s) outside the middleware. 另一个可能的方法是将路由放置在中间件外部,并且通常是在构建没有视图的API时又收到此错误。

In my case I wanted to avoid this error for all my api routes 'api/*' : 就我而言,我想避免所有api路由'api/*'出现此错误:

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        'api/*',
    ];
}

You can locate the file at app/Http/Middleware/VerifyCsrfToken.php 您可以在app/Http/Middleware/VerifyCsrfToken.php找到文件

You should be aware that you can do this for web routes too, it's not the common practice but there are some cases where you don't have other option, as Laravel Doc says: csrf-excluding-uris 您应该意识到,您也可以对Web路由执行此操作,这不是常见的做法,但是在某些情况下您没有其他选择,如Laravel Doc所说: csrf-clusion-uris

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:laravel 5.2中VerifyCsrfToken.php第67行中的TokenMismatchException - Error: TokenMismatchException in VerifyCsrfToken.php line 67 in laravel 5.2 Laravel 5.2-VerifyCsrfToken.php 67行中的TokenMismatchException: - Laravel 5.2 - TokenMismatchException in VerifyCsrfToken.php line 67: VerifyCsrfToken.php 67行中的TokenMismatchException:在Laravel 5.2中 - TokenMismatchException in VerifyCsrfToken.php line 67: in Laravel 5.2 VerifyCsrfToken.php 67行中的TokenMismatchException:Laravel 5.2 - TokenMismatchException in VerifyCsrfToken.php line 67: Laravel 5.2 Laravel 5-VerifyCsrfToken.php 67行中的TokenMismatchException - Laravel 5 - TokenMismatchException in VerifyCsrfToken.php line 67 PHP Laravel:VerifyCsrfToken.php 67行中的TokenMismatchException - PHP Laravel : TokenMismatchException in VerifyCsrfToken.php line 67 使用ajax在laravel上的VerifyCsrfToken.php第67行中的TokenMismatchException - TokenMismatchException in VerifyCsrfToken.php line 67 on laravel using ajax VerifyCsrfToken.php 67行中的错误TokenMismatchException: - Error TokenMismatchException in VerifyCsrfToken.php line 67: 如何解决VerifyCsrfToken.php第67行中的TokenMismatchException - How to solve TokenMismatchException in VerifyCsrfToken.php line 67 VerifyCsrfToken.php Laravel 5.2中的TokenMismatchException - TokenMismatchException in VerifyCsrfToken.php Laravel 5.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM