Reputation: 87
I cant get pass this one, im try to do ajax request in laravel and i cant get pass this error:
{message: "", exception: "Symfony\Component\HttpKernel\Exception\HttpException",…}
What does this mean exactly ? CSRF-TOKEN token error or ?
ajax request:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$("#send").click(function(e){
e.preventDefault();
var id = 5;
$.ajax({
url: '{{url("fly")}}',
type:'POST',
data:{id:id},
success:function(data){
console.log('Yes' + data);
}
});
});
Any idea ?
Upvotes: 0
Views: 2646
Reputation: 31
Adding the below CSRF field within the form tag will solve the issue:
{{ csrf_field() }}
Upvotes: 0
Reputation: 8078
Your issue is with CSRF token
You have added CSRf token in ajax Header but in meta you have not specified it. Add crsf meta in your layout file
<meta name="csrf-token" content="{{ csrf_token() }}">
if You check your Browser network console then you will see 419 error if you are csrf token is not passing properly
Request URL: http://127.0.0.1:8000/fly
Request Method: POST
Status Code: 419 unknown status
Remote Address: 127.0.0.1:8000
Referrer Policy: no-referrer-when-downgrade
updated header as below since your header token is not set in on document ready nor in on click send
$("#send").click(function(e){
e.preventDefault();
var id = 5;
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
url: '{{url("fly")}}',
type:'POST',
data:{id:id},
success:function(data){
console.log('Yes' + data);
}
});
});
Upvotes: 2
Reputation: 164
I already told you yesterday do this:
$("#send").click(function(e){
e.preventDefault();
var id = 5;
$.ajax({
url: '{{url("fly")}}',
type:'POST',
data:{'id':id,'_token': "{{ csrf_token() }}"},
success:function(data){
console.log('Yes' + data);
}
});
});
Upvotes: -1