laravel TokenMismatchException在Ajax请求 [英] laravel TokenMismatchException in ajax request

查看:1818
本文介绍了laravel TokenMismatchException在Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的资源组,并使用此过滤器来解决 TokenMismatchException 问题:

 路线::过滤器(CSRF',函数($路径,$请求){
    如果(strtoupper($请求 - >实现getMethod())==='GET'){
        返回;
        //获取请求不CSRF保护
    }

    $令牌= $请求 - >阿贾克斯()? $请求 - >标题(X-CSRF令牌):输入::得到('_令牌');

    如果(会话::令牌()!= $令牌){
        抛出新照亮\会议\ TokenMismatchException;
    }
});
 

我的路线:

 路线::组(阵列('preFIX'=> =&GT'前''管理​​';'CSRF'),函数(){
    路线::资源(个人资料,ProfileController可,阵列('为'=>'个人资料'));
});
 

现在。我得到错误的Ajax请求,如本code:

 <脚本类型=文/ JavaScript的>
    $(文件)。就绪(函数(){
       $('#FRM)。递交(函数(五){
           即preventDefault();
           NAME = $('#姓名)VAL()。
           家庭= $('#家庭)VAL()。
           电子邮件= $('#邮件)VAL()。
           currPassword = $('#currPassword)VAL()。
           密码= $('#密码)VAL()。
           。password_confirmation = $('#password_confirmation)VAL();

           $。员额({{航线('admin.profile.update',$简介 - > ID)}},
                {
                  _method:把',
                  名称:名称,
                  家庭:家庭,
                  电子邮件:电子邮件,
                  currPassword:currPassword,
                  密码:密码,
                  password_confirmation:password_confirmation
                },
                功能(数据)
                {
                    警报(data.errors.name);
                },'JSON');
                返回false;
       });
});
< / SCRIPT>
 

ERROR:

<$p$p><$c$c>{"error":{"type":"Illuminate\\Session\\TokenMismatchException","message":"","file":"\/var\/www\/alachiq\/app\/filters.php","line":83}}

我想我一定要在 $发送_token。交。但我不能让输入标记名称属性。的iget此错误:

 类型错误:升压叫不实现接口HTMLInputElement的对象。
 

解决方案

您必须插入一个隐藏的输入与_token,后来获得了价值,你做的就是在你的ajax帖子的其他表单字段。

 &LT;输入类型=隐藏名称=_标记值={{csrf_token()}}/&GT;
 

的另一种方法,

在你的看法,你可以设置一个对象的_token

 &LT;脚本类型=文/ JavaScript的&GT;
    VAR _globalObj = {{json_en code(阵列('_令牌'=&GT; csrf_token()))}}
&LT; / SCRIPT&GT;
 

和后来的Ajax调用,您可以从这样的对象得到_token:

  VAR令牌= _globalObj._token;
 

和包括它在你的ajax帖子。

i'm using resource group and use this filter to resolve TokenMismatchException problem:

Route::filter('csrf', function($route, $request) {
    if (strtoupper($request -> getMethod()) === 'GET') {
        return;
        // get requests are not CSRF protected
    }

    $token = $request -> ajax() ? $request -> header('X-CSRF-Token') : Input::get('_token');

    if (Session::token() != $token) {
        throw new Illuminate\Session\TokenMismatchException;
    }
});

my route :

Route::group(array('prefix'=> 'admin', 'before' => 'csrf'), function(){
    Route::resource('profile' , 'ProfileController', array('as'=>'profile') );
});

now. i get error to Ajax requests such as this code:

<script type="text/javascript">
    $(document).ready(function() {
       $('#frm').submit(function(e){
           e.preventDefault();
           name         = $('#name').val();
           family       = $('#family').val();
           email        = $('#email').val();
           currPassword = $('#currPassword').val();
           password     = $('#password').val();
           password_confirmation = $('#password_confirmation').val();     

           $.post("{{ route('admin.profile.update', $profile->id) }}",
                { 
                  _method : 'PUT',
                  name                  : name,
                  family                : family,
                  email                 : email,
                  currPassword          : currPassword,
                  password              : password,
                  password_confirmation : password_confirmation  
                },
                function(data)
                {
                    alert(data.errors.name);
                },'json');
                return false;
       });
});
</script>

ERROR:

{"error":{"type":"Illuminate\\Session\\TokenMismatchException","message":"","file":"\/var\/www\/alachiq\/app\/filters.php","line":83}}

i think i'm must be sent _token in $.post. but i can not get input tag with name attribute. iget this error:

TypeError: 'stepUp' called on an object that does not implement interface HTMLInputElement.

解决方案

You have to insert a hidden input with the _token and later get that value as you do to get the other form fields in your ajax post.

<input type="hidden" name="_token" value="{{ csrf_token() }}" />

An another method,

On your view you can set an object with the _token

<script type="text/javascript">
    var _globalObj = {{ json_encode(array('_token'=> csrf_token())) }}
</script>

and later on your ajax call you can get the _token from the object like this:

var token = _globalObj._token;

and include it on your ajax post.

这篇关于laravel TokenMismatchException在Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆