Level 10
Give this blog post a read, should answer any questions you have: http://engageinteractive.co.uk/blog/csrf-protection-with-ajax-and-laravel
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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>
Please or to participate in this conversation.