7

Whenever I send a POST request to server, TokenMismatchException error comes. I have already tried sending

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

Earlier, I was using ajaxHeader to send this particular piece of information to server but that is also throwing same error.

I have debugged more and find out that in VerifyCsrfToken file.

protected function tokensMatch($request)
    {
        $token = $this->getTokenFromRequest($request);
        return is_string($request->session()->token()) &&
               is_string($token) &&
               hash_equals($request->session()->token(), $token);
    } 

array:3 [
 "sessionToken" => "rgicYLOUhb2kLLChpVByNLQO1KVMb0Gkjzb7ZtTN" //$request->session()->token()
 "requestToken" => "IgXWquvnfujZJ1Vs9vbSgpjgX3rAnd5PpeklRvBD"  // $request->input('_token') ?: $request->header('X-CSRF-TOKEN')
 "laravel_token" => "rgicYLOUhb2kLLChpVByNLQO1KVMb0Gkjzb7ZtTN" //csrf_token()
]

I am getting above array in middleware token match function. Can anybody please tell me the reason and solution of this particular problem ? Below is the ajax I am using

function likeReview(id)
    {
        var like_span = $('#like_'+id);
        var like_div  = $('#likeDiv_'+id);
        var like_span_text = $('#likeText_'+id);
        $.ajax({
            type: 'post',
            url: '{{route('like.review')}}',
            data: {review_id: id},
            beforeSend: function () {
            },
            success: function (data) {
                if(data.status == 'success')
                {
                    var like = like_span.html();
                    var sum  = 0;
                    if(data.like == 1){
                        sum = parseInt(like)+1;
                        like_div.addClass('upvoted-active');
                        like_span_text.html('UPVOTED');
                    } else {
                        sum = parseInt(like)-1;
                        like_div.removeClass('upvoted-active');
                        like_span_text.html('UPVOTE');
                    }
                    like_span.html(sum);
                }
            },
            error: function (xhr, textStatus, thrownError) {
                alert('Something went wrong. Please try again!');
            }
        });
    }

Function is called on click of upvote button

<div class="js-btn-thank-area upvoted-active js-activity-root" id="likeDiv_{{$review->id}}">
    <a href="javascript:;" onclick="likeReview({{$review->id}})" class="thank-btn">
    <i class="fa fa-arrow-up fa-fw"></i>
    <span class="feed-action-text" id="likeText_{{$review->id}}">UPVOTED</span>
   </a>
   <div class="stats-thanks" id="like_{{$review->id}}">                                                                    
    {{$review->likes()->where('like','=',1)->count()}}
   </div>
 </div>
11
  • post your code. how do you send data ? Commented May 8, 2017 at 7:24
  • 1
    Post the form submit and ajax code where you are facing the problem ? Commented May 8, 2017 at 7:26
  • 2
    Have you looked through laravel.com/docs/5.4/csrf#csrf-x-csrf-token Commented May 8, 2017 at 7:32
  • are you using a form for posting it? Commented May 8, 2017 at 7:34
  • 1
    I meant to say that Ajax doesn't use the form but does its own post. I think you need to change data: {review_id: id}, to data: {review_id: id, _token: {{ csrf_token() }}},. Nigel's answer should work, too. Commented May 8, 2017 at 9:50

1 Answer 1

2

Just using CSRF as a field for posting with AJAX does not work;

$.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });

Before you make the ajax call set it up :)!

EDIT: You can also put in the data part of your ajax request;

data: {
    review_id: id,
    "_token": "{{ csrf_token() }}"
}

EDIT: To clarify clearing temporary data from storage solved this issue in chat.

Sign up to request clarification or add additional context in comments.

8 Comments

Already tried both of the way you mentioned it's still not working.
Did you remove the csrf token from the html form?
I didn't get you can you be more specific ?
remove this: <input type="hidden" name="_token" value= "{{csrf_token()}}"> then use one of my changes and print out response if you still have issues.
I don't have enough point to chat. I am working on it. My colleague is on the chat reply him
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.