Hi Developers ,
The main reason for facing the 500 error in the laravel project is no adding the @csrf field which prevents the cross site scripting. Since laravel is highly concerned about the security it outbounds all the request without the csrf token. In normal for submission we will add @csrf field that will store the csrf token and sends it to backend .
But , While sending any request using AJAX we should need the csrf token . For that we are should generate csrf token using meta tag and we should add it in request , lets see how its done
Lets Add meta tag in the header section to generate the csrf token
<meta name="csrf-token" content="{{ csrf_token() }}">
Lets add the ajax code for sending the request without reloading the page . We should add the csrf token in the headers section of the ajax request that generated in the meta tag before sending the request
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.
Be the first to know about releases and tutorial news and solutions.
We care about your data in our privacy policy.
ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.
Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides