Laravel 5: Ajax Post 500 (Internal Server Error) - Fix

November 11, 2017 | Category : Ajax JQuery Laravel PHP

If you are fetching 500 internal server error in jquery ajax post request in laravel 5, then you are a right place. Here i will let you know how to fix 500 (internal server error) ajax post request in laravel 5.

If you know well laravel then you know about csrf token, laravel provide best security using csrf token. So you have each time pass csrf_token when you fire ajax post, delete or put request. You can generate csrf token using csrf_token() helper of laravel 5.

So, Here i will see you how to generate csrf_token and pass on each ajax request of jquery. So let's you have to add bellow meta tag and then you have to simple pass headers. It will automatically pass token on each post request.

Add Meta tag:

<meta name="csrf-token" content="{{ csrf_token() }}">

Add JS Code:

$.ajaxSetup({

headers: {

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

}

});

As above both code, you have to write in each page so you can simply put in layout page.

I hope you found your best solution...