This question has been asked before but non of the solutions work for me. This my code
<meta name="csrf-token" content="{{csrf_token()}}">
and the script
<script type="text/javascript">
$(document).ready(function () {
$(".comment-rate-wrapper a img").on('click', function (e) {
e.preventDefault();
var item_id = 1;
var url = "{{route('like.voteHandler', ':id')}}";
url = url.replace(':id', item_id);
//alert(url); I am sure thr url is correct and it outputs correctly
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
method: 'POST', // Type of response and matches what we said in the route
url: url, // This is the url we gave in the route
data: {
'item_id': item_id
},
success: function (result) { // What to do if we succeed
console.log(result);
},
error: function (jqXHR, textStatus, errorThrown) { // What to do if we fail
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
})
})
</script>
and this is the controller function
public function voteHandler($item_id)
{
echo "sas";
return "hi";
}
But it always returns an empty array and I have no idea why it is not working.
Thanks