Laravel VerifyCsrfToken异常无法正常工作

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/*'
    ];
}

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/*'
    ];
}