dtest84004 2015-09-23 23:25
浏览 74
已采纳

Laravel VerifyCsrfToken异常无法正常工作

I'm trying to make a simple Ajax post using Laravel 5. I read that there is a issue with the Csrf Token matching and that i could put my uri into the VerifyCsrfToken expection to step around this. Did I mess something up in my code? How can I get this working? Here is what I have:

HTML:

<div id = "event-carousel" class = "carousel slide">

                                                <div id = "event-news" class = "carousel-inner">
                                                    {{--*/ $isFirst = true; /*--}}
                                                    @foreach($events as $event)
                                                        <div class="item{{{ $isFirst ? ' active' : '' }}}">
                                                          <div class = "foto we">
                                                            <img src ="/assets/image/{{$event->photo}}" alt = "intexhange" class= "img-responsive">
                                                          </div>
                                                          <div class = "mask">
                                                                <h1>{{$event->title}}</h1>
                                                                <div class = "secinfo">
                                                                    <p>{{$event->teacher}}</p>
                                                                    <p>{{$event->location}}</p>
                                                                    <p>{{$event->published_at}}</p>
                                                                </div>
                                                              <div class = pitch>
                                                                <p class = "subhead-how">{{$event->description}}</p>
                                                                 {!!Form::open(array('url' => 'book')) !!}
                                                                      {!! Form::hidden('title', $event->title, ['class' => 'form-control']) !!}
                                                                      {!! Form::hidden('user_id', $myid, ['class' => 'form-control']) !!}
                                                                      <input type="hidden" id="token" value="{{ csrf_token() }}">
                                          <span href="#event-carousel" data-slide="prev"class = "glyphicon glyphicon-arrow-left"></span>{!!Form::button('Join Activity', array('type' => 'submit', 'class' => 'btn btn-danger jact'))!!}<span href="#event-carousel" data-slide="next" class = "glyphicon glyphicon-arrow-right"></span>
                                                                  {!! Form::close() !!}
                                                              </div>
                                                          </div>
                                                    {{--*/ $isFirst = false; /*--}}
                                                    </div>
                                                    @endforeach
                                            </div>
                                           </div>

JS:

$('.jact').click(function(e){
   e.preventDefault();


   var title = $(this).find('input[title=title]').val();
   var user_id = $(this).find('input[user_id=user_id]').val();

   $.post('book', {title: title, user_id: user_id}, function(data){
       console.log(data);
   })

});

Controller:

 public function book()
{

    if(Request::ajax()){

        return Response::json(Input::all());

    }
}

VerifyCsrfToken:

  class VerifyCsrfToken extends BaseVerifier
{

    protected $except = [
        'book/*'
    ];
}
  • 写回答

1条回答 默认 最新

  • douwo4837 2015-09-24 04:14
    关注

    The $except functionality uses the Illuminate\Http\Request::is() method. It loops through the $except array and tests the current request against each entry. If one matches, it will skip the verification.

    From the code, it looks like you're posting to book, not a url under book. If you were to call $request->is('book/*'), it would return false, since you're not at a url under book.

    If you would like to ignore book, and all of its descendants, you will want this:

    class VerifyCsrfToken extends BaseVerifier
    {
        protected $except = [
            'book',
            'book/*'
        ];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大