【问题标题】:Laravel 5 - TokenMismatchException in VerifyCsrfToken.phpLaravel 5 - VerifyCsrfToken.php 中的 TokenMismatchException
【发布时间】:2016-05-16 17:57:01
【问题描述】:

我为我的网站创建了一个简单的 PUT 表单(我还有 GET 和 POST 表单,它们都可以正常工作)。

<h1>Edit {!! $brands->brand_name !!}</h1>

    <!-- if there are creation errors, they will show here -->
    {!! Html::ul($errors->all()) !!}

    {!! Form::model($brands, array('route' => array('brands.update', $brands->id), 'method' => 'PUT', 'files' => true)) !!}

    <div class="form-group">
        {!! Form::label('brand_name', 'Name') !!}
        {!! Form::text('brand_name', null, array('class' => 'form-control')) !!}
    </div>

    <div class="form-group">
        {!! Form::label('brand_name_rus', 'Name_rus') !!}
        {!! Form::text('brand_name_rus', Input::old('brand_name_rus'), array('class' => 'form-control')) !!}
    </div>

    <div class="form-group">
        {!! Form::label('Your image') !!}
        <img src="http://parasha.dev/{{ $brands->img_brand_path }}"/>
    </div>

    <div class="form-group">
        {!! Form::label('img_brand_path', 'New image') !!}
        {!! Form::file('img_brand_path') !!}
    </div>

    {!! Form::submit('Edit the Brand!', array('class' => 'btn btn-primary')) !!}

    {!! Form::close() !!}

我还使用 Clip Two Theme 作为包含这些表单的管理面板的布局。当我同时使用两者时(布局和我的表单,它给了我一个错误-> VerifyCsrfToken.php 中的 TokenMismatchException)。所以这个引导主题不知何故会导致该错误。

所以我有两个问题。首先,我该如何解决这个问题。第二,我可以在没有令牌自动创建的情况下创建 laravel 表单吗?对不起我的英语)

【问题讨论】:

    标签: forms twitter-bootstrap laravel token mismatch


    【解决方案1】:

    您的表单中似乎没有包含 csrf_token。

    {!! csrf_field() !!} 添加到您的表单中。如果您使用 ajax 请求,您可以将 {!! csrf_field() !!} 放在“主”模板中,并在 ajax 标头中引用它,如下所示:

    $.ajaxSetup({
    headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content')}
    });
    

    【讨论】:

    • 这个字符串创建我的令牌值。在检查员中,我发现了这个价值。 {!! Form::model($brands, array('route' =&gt; array('brands.update', $brands-&gt;id), 'method' =&gt; 'PUT', 'files' =&gt; true)) !!} 问题不在于形式,我的引导主题导致该错误!
    【解决方案2】:

    这也有效:

    {!! Form::token() !!}
    

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 2015-08-19
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 2015-05-06
      • 2016-03-20
      相关资源
      最近更新 更多