I want to send a short form with email by ajax.
Code below adds a hidden input with name='_token'
{!! Form::open(['route'=>'registerCheck', 'id'=>'register_form', 'novalidate'=>'novalidate']) !!}
In js script I add data to request:
$.ajax({
method:'POST',
url: $form.attr('action'),
data:{
'_token': $form.find('[name="_token"]').val(),
email: $('#email').val(),
user: $this.attr('id'),
}
})
How can I authenticate it and which namespaces should I include to do this?
First add this meta
<meta name="csrf-token" content="{{ csrf_token() }}" />
then in your script
add this
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
finally change your request to this
$.ajax({
method:'POST',
url: $form.attr('action'),
data:{
_token: CSRF_TOKEN,
email: $('#email').val(),
user: $this.attr('id'),
}
})