我正在获得令牌不匹配异常,每件事似乎都是正确的。

这是我的查看代码

@extends('layouts.master')
@section('content')
<div class="bg"></div>
<div class="login-sec clearfix">

    <h1 class="lg-logo">YOU ARE JUST ONE STEP AHEAD</h1>
    <div class="login-box clear">

        <form id="websiteform" action="#">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <div class="col-md-12 form-field">
                <label for="">Project Name</label>
                <input type="text" name="project" id="project">
            </div>

            <div class="col-md-12 form-field">
                <label for="">Website URL</label>
                <input type="url" name="website_url" id="website_url">
            </div>

            <div class="col-md-12 form-field">
                <button type="submit" class="btn-green btn-large">Add Your Website</button>
            </div>
        </form>

    </div>
</div>
@stop


并形成源头节和页脚为

  <head>
   <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<meta name="csrf-token" content="4ryCSznz0mPSBcwXvbmZZHkZGcxZpyDy2dQ1VAoc" />
<link href="http://localhost:8080/css/bootstrap.min.css" rel="stylesheet" media="screen">




 <script src="http://localhost:8080/js/jquery.min.js"></script>
<script src="http://localhost:8080/js/bootstrap.min.js"></script>
<script src="http://localhost:8080/js/main.js"></script>


和我在main.js中的ajax调用

$(document).ready(function () {

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
$('#websiteform').submit(function(e){
    e.preventDefault();
    var pro=$('#project').val();
    var url=$('#website_url').val();

    $.ajax({
        type: "POST",
        url: "project_save",
        data:  {project: pro, url: url},
        success: function(data)
        {
            if(data.success==false)
            {
             alert(data.error);
            }
            else{
                window.location =  "dashboard";
            }
        },
        error:function(){}
    });
});
});


以及路线和控制器功能

 Route::post('/project_save','WebsiteController@project_save');
  public function project_save()
{
    if(Request::ajax()){

        $validator = Validator::make(Request::all(), [
            'project' => 'required|max:255',
            'url' => 'required',
        ]);
        if($validator->fails()){
            return Response::json(['success'=>false,'error'=>$validator->errors()->toArray()]);
        }

        $Website = new Website;
        $Website->project_name = Request::get('project');
        $Website->website_url = Request::get('url');
        $Website->user_id = Auth::id();
        $Website->save();
        return Response::json(['success'=>true]);
    }

}


这是我的代码,我正在获得tokenmismatach例外。

最佳答案

更改您的\App\Http\Middleware\VerifyCsrfToken.php,将此代码添加到功能tokensMatch中:

if ($request->ajax()) {
        return true;
    }

关于javascript - Laravel 5.2 Ajax调用中的TokenMismatchException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35790280/

10-10 21:16