Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lars-Janssen's avatar

TokenMismatchException in VerifyCsrfToken.php line 53:

Where do I've to add the token in my ajax request? This is my code:

$(document).ready(function () { var xhr; });
$("#test").change(function(e) {

csrf = $("#token").attr('content')
option = $(this).val();

  $.ajax({
      url: '/public/receiveuserinformation',
      type: 'POST',
      data: { option_id: option },
      beforeSend: function(xhr){xhr.setRequestHeader('X-CSRF-TOKEN', csrf);},
      success: function(result) {
          $("#kilometersprive").val(result);
      }
  });

});

I receive the error: TokenMismatchException in VerifyCsrfToken.php line 53:

This is my html:

 <div class="form-group">
                    <label for="content" class="col-lg-2 control-label">Standaard route</label>
                    <div class="col-lg-10">
                            <select class="form-control input-sm" name="test" id="test">
                            @foreach($standaardroute as $route)
                                <option value="{!! $route->id !!}">{!! $route->van !!} - {!! $route->naar !!}</option>
                            @endforeach
                            </select>               
                    </div>
                </div>
0 likes
2 replies
SamKitano's avatar

For that kind of ajax request you need to set a meta-tag in the head section of your page:

<meta id="#token" name="csrf-token" content="{{ csrf_token() }}" />

Edit: for some reason, the forum is not displaying the content section of the meta-tag above. It should be crsf_token() enclosed on double curly brackets.

Please or to participate in this conversation.