TokenMismatchException in VerifyCsrfToken.php line 53:


TokenMismatchException in VerifyCsrfToken.php line 53:

好吧,我的表格有点问题,我尝试了很多方法都不起作用当我单击发送时,它给了我此错误

TokenMismatchException in VerifyCsrfToken.php line 53:
好的,我

明白了,我放<input type="hidden" name="_token" value="{{ csrf_token() }}">

<form action="" method="post" class="landing-form">

然后我得到了这个错误

MethodNotAllowedHttpException in RouteCollection.php line 219:

这是我的表单代码

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>landin page</title>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script> 
    <script type="text/javascript" href="{{asset('js/class.FormValidation.js')}}"></script>
    <script type="text/javascript" href="{{asset('js/landin_validation.js')}}"></script>
    <link rel="stylesheet" type="text/css" href="{{asset ('js/style.css')}}"/>
  </head>
  <body>
    <div class="page-wrapper">
      <div class="header-wrapper">
        <div class="header-content">Company Logo Here</div>
      </div>
      <div class="content">
        <h1>Advertising Header Goes Here!</h1>
        <p>
          There are many variations of passages of Lorem Ipsum available,<br/> 
          but the majority have suffered alteration in some form, by injected humour, <br/> 
          or randomised words which don't look even slightly believable. <br/> 
          If you are going to use a passage of Lorem Ipsum, <br/> 
          you need to be sure there isn't anything embarrassing hidden in the middle of text. <br/> 
          All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, <br/> 
          making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, <br/> 
          combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. <br/> 
          The generated Lorem Ipsum is therefore always free from repetition, injected humour,<br/>  
          or non-characteristic words etc.
        </p>
        <div class="form-wrapper">
          <form action="" method="post" class="landing-form">    
              <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <label>Fill your details here - </label><br/><br/>
            <input placeholder="Full name:" type="text" name="name" class="field-name" />
            <input placeholder="Email:" type="text" name="email" class="field-email" />
            <input placeholder="Phone Number:" type="text" name="phone" class="field-phone" />
            <input type="submit" name="submit" value="Send" />
          </form>
        </div>
      </div>
      <div class="footer">2014 &copy; My Compay Name</div>
    </div>
  </body>
</html>

这也是我的课。表单验证.js

function FormValidation(){
  this.nameReg = [
    /^([a-zA-Z's]+){2,255}$/
  ];
  this.emailReg = [
    /^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
  ];
  this.phoneReg = [
    /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/i,
    /^[0-9]{3}.[0-9]{3}.[0-9]{4}$/i,
    /^'([0-9]{3}')-[0-9]{3}-[0-9]{4}$/i,
    /^[0-9]{9,10}$/i
  ];
  this.testName = function( nameInput ){
    return this.inputCheck(this.nameReg, nameInput);
  };
  this.testEmail = function( emailInput ){
    return this.inputCheck(this.emailReg, emailInput);
  };
  this.testPhone = function( phoneInput ){
    return this.inputCheck(this.phoneReg, phoneInput);
  };
  this.inputCheck = function( regArray, inputData ){
    var valid = false;
    $.each( regArray, function( key, val ){
      if( val.test( inputData ) ){
        valid = true;
      }
    });
    return valid;
  };
}

这是路线:

Route::get('contact', 'PagesController@contact');

这是页面控制器:

public function contact(){
    self::$data['title'] = 'Contact us';
    return view('content.contact',self::$data);
}

真的希望你帮助我,谢谢:)

看起来没有可用的路由,用于向您的联系人路由发送 POST 请求。正如你所说:

Route::get('contact', 'PagesController@contact');

是到达表单的路由,但 Laravel 无法使用您从表单发送的 POST 请求来验证 GET 请求。因此,您需要第二条路由(和方法)来处理 POST 请求:

Route::post('contact', 'PagesController@postContact');

你仍然需要在 PagesController 上使用一个 postContact() 方法来处理 post 请求。

附言你也可以使用这个:

{!! csrf_field() !!}

这会将标记字段的 HTML 代码添加到表单中,而无需键入隐藏字段的任何内容。


附言你也可以使用这个:

Route::any('/contact', 'PagesController@contact');

但这需要你在 contact() 方法中执行逻辑(将 POST 与 GET 请求分开)。