即使在Laravel 5

时间:2015-11-12 08:49:31

标签: php ajax laravel-5.1

我试图在laravel 5中使用ajax发布。 我得到CSFR令牌不匹配异常。 我环顾四周,发现可能有这样的问题,但在这种情况下,我确实包含了所有必要的代码。

请查看我的代码。

查看

{!! Form::open(array('url'=>'admin/index','method'=>'POST', 'id'=>'addrole')) !!}
<div class="control-group">
  <div class="controls">
     {!! Form::text('user','',array('id'=>'','class'=>'form-control span6','placeholder' => 'Username')) !!}
  </div>
</div>
<div class="control-group">
  <div class="controls">
      {!! Form::text('role','faculty',array('class'=>'form-control span6', 'placeholder' => 'User Role')) !!}
      <input type="hidden" name="_token" value="{{ csrf_token()}}">
  </div>
</div>
{!! Form::button('Submit', array('class'=>'send-btn')) !!}
{!! Form::close() !!}

<script type="text/javascript">

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

    $(document).ready(function(){
        $('.send-btn').click(function(){
            $.ajax({
                url: 'index',
                type: "post",
                data: {
                    '_token': $('input[name="_token"]').val(),
                    'user':$('input[name=user]').val(),
                    'role':$('input[name=role]').val()
                }, //, '_token': $('input[name=_token]').val()
                success: function(data) {
                    alert(data);
                }
            });
        });
    });
</script>

我在隐藏的输入中提供了令牌,我在帖子中包含了这个。

我确实有一个元头如下。

<meta name="_token" content="{!! csrf_token() !!}"/>

以下是我的控制器。

public function index()
{
    if(Request::ajax()) {
        $data = Input::all();
        print_r($data);die;
    }
}

这个只是用于测试,这里没什么。 我的路线如下

Route::get('admin/index', function() {
  return View::make('admin');
});
Route::post('admin/index', 'AdminController@index');

请帮我解决此问题

这是日志中的输出。

[2015-11-12 09:04:31] local.ERROR: exception 'Illuminate\Session\TokenMismatchException' in /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php:46
Stack trace:
#0 /var/www/html/vidman-laravel/app/Http/Middleware/VerifyCsrfToken.php(17): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#1 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): App\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#2 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(55): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#3 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#4 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(61): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#5 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#6 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(36): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#7 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#8 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(40): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(42): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#11 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(125): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#12 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#13 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(101): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#14 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(115): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#15 /var/www/html/vidman-laravel/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(84): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#16 /var/www/html/vidman-laravel/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#17 {main} 

以下是我的浏览器控制台帖子标题,并且页内的csrf标记不匹配。请看下面

The token in the page ==> input type="text" name="_token" value="{{ csrf_token()}}"

Browser post header

1 个答案:

答案 0 :(得分:1)

我认为当你使用

{!!  Form::open() !!}

这已经包含了表单输入标记中的csrf_token。因此,没有必要包含在meta标记或input标记中。 例如: 当你这样做时:

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

您的表单标记如下:

<form action="your action" method="POST" accept-charset="UTF-8">
     <input type="hidden" value="token_string" name="_token" > 
</form>

现在在你的js文件中验证你可以这样做的令牌:

$.ajaxSetup({
    headers:{
        'X-CSRF-Token' : $("input[name='_token'").attr('value')
    }
});