<script>
$(document).ready(function() {
$(document).on('click', '#ajax', function () {
$.ajax({
type:'POST',
url:'/ajax',
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
success:function(data){
$("#msg").html(data.msg);
}
});
});
});
</script>
<!-- Add this to header -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<!-- Add this to script -->
<script>
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
</script>
"_token": "{{ csrf_token() }}",
$.ajax({
url: '/postAjaxUrl',
type: 'POST',
dataType: 'json',
data: {user_id: 10},
success: function(response) {
console.log(response);
},
beforeSend: function (request) {
return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content'));
}
});