【问题标题】:No solution for TokenMismatchException in VerifyCsrfToken.php line 53 in Laravel 5.1Laravel 5.1 中的 VerifyCsrfToken.php 第 53 行中的 TokenMismatchException 没有解决方案
【发布时间】:2015-08-27 16:32:11
【问题描述】:

我是 laravel 框架 5.1.10 版的新手。最近我正在尝试为数据库中的 CRUD 制作一个简单的项目。我正在尝试将数据从视图发送到控制器。但我收到了这个错误。我尝试从堆栈和 laravel.io/forum 找到我的解决方案但失败了。这就是为什么我需要你的建议。 我的 routes.php 代码如下

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('welcome');
});
Route::get('/index','all_control@index');
Route::post('/insert_students','all_control@insert_students');

Route::get('/form',array(
    'as' => 'getform',
    'uses' => 'all_control@getform'
));

Route::post('/form',array(
    'as' => 'postform',
    'uses' => 'all_control@postform'
));

我的 form.blade.php 如下所示

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Title</title>
        <meta charset="UTF-8">
        <meta name=description content="">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="csrf-token" content="{{ csrf_token() }}">
        <!-- Bootstrap CSS -->
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" media="screen">
    </head>
    <body>
    <div class="container">
        <h1>Form</h1>
        <h3>
            <?php
                if(Session::has('message'))
                    {
                        echo Session::get('message');
                    }
            ?>
        </h3>
        <form action="{{ URL::route('postform') }}" method="post" role="form">
            <legend>Form Title</legend>

            <div class="form-group">
                <label for="">Roll</label>
                <input type="text" class="form-control" name="roll" id="roll" placeholder="Input...">
            </div>

            <div class="form-group">
                <label for="">Marks of Physisc CT</label>
                <input type="number" class="form-control" name="ct" id="ct" placeholder="Input...">
            </div>

            <button type="submit" class="btn btn-primary">Hit the Button</button>
        </form>
    </div>


    </body>
</html>

我的 all_control.php 在这里

<?php

namespace App\Http\Controllers;

use App\Model\students;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class all_control extends Controller
{


    public function getform()
    {
        return view('form');
    }
    public function postform()
    {
        $roll = Input::get('roll');
        $ct_number = Input::get('ct');
        Session::flash('message','You have done successfully!!!!');
        return Redirect::route('getform');
    }
}

【问题讨论】:

    标签: laravel-5.1


    【解决方案1】:

    添加这个

    &lt;input type="hidden" name="_token" value="{{ csrf_token(); }}"&gt;

    打开表格后

    <form action="{{ URL::route('postform') }}" method="post" role="form">
       <input type="hidden" name="_token" value="{{ csrf_token(); }}">
       <legend>Form Title</legend>
    
       <div class="form-group">
          <label for="">Roll</label>
          <input type="text" class="form-control" name="roll" id="roll" placeholder="Input...">
       </div>
       <div class="form-group">
           <label for="">Marks of Physisc CT</label>
           <input type="number" class="form-control" name="ct" id="ct" placeholder="Input...">
       </div>
       <button type="submit" class="btn btn-primary">Hit the Button</button>
    </form>
    

    【讨论】:

    • 我无法理解您的“刚打开表格”。请粘贴您的代码。谢谢。
    • Class 'App\Http\Controllers\Session' not found --- 你能告诉我解决方案吗?
    • 那将不得不在新问题中
    【解决方案2】:

    上周我也遇到了这个错误,但是当我现在切换到 ajax 帖子时它工作正常,我不知道在 php 帖子中,但你可以尝试:

    //必须在head标签中

    <meta name="csrf-token" content="{{ csrf_token() }}">
    
    <script type=text/javascript>
    $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
    });
    </script>
    

    【讨论】:

      猜你喜欢
      • 2015-09-05
      • 2016-06-29
      • 2016-03-03
      • 2016-09-02
      • 2016-03-20
      • 2015-11-07
      • 2016-01-02
      • 2017-07-20
      • 1970-01-01
      相关资源
      最近更新 更多