DEV Community

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

500 Internal Server Error In Laravel 9 AJAX

In this article, we will see 500 internal server errors in laravel 9 ajax.

Also, we can see how to solve or fixed laravel 9 500 internal server error ajax.

If you are fetching 500 internal server errors in jquery ajax post request in laravel 9.

Here we will let you know how to fix the ajax post 500 (Internal Server Error) request in laravel 9.

Laravel provides the best security using the csrf token. So, you have each time pass csrf_token when you fire ajax in the post, delete or put a request.

You can generate csrf token using the csrf_token() helper of laravel 9. we will see you how to generate csrf_token and pass on each ajax request of jquery.

Also, you can add below meta tag and then you have to simply pass headers. It will automatically pass a token on each post request.

Add Meta tag:

<meta name="csrf-token" content="{{ csrf_token() }}">
Enter fullscreen mode Exit fullscreen mode

Add JS Code:

$.ajaxSetup({

headers: {

'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

}

});

Enter fullscreen mode Exit fullscreen mode

As above both codes, you have to write on each page. So, you can simply put it on the layout page or common header page.

Top comments (0)