Ajax post request in laravel 5 return error 500 (Internal Server Error)
While this question exists for a while, but no accepted answer is given I'd like to point you towards the solution. Because you're sending with ajax, and presumably still use the CSRF middleware, you need to provide an additional header with your request.
Add a meta-tag to each page (or master layout): <meta name="csrf-token" content="{{ csrf_token() }}">
And add to your javascript-file (or section within the page):
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
See https://laravel.com/docs/master/csrf#csrf-x-csrf-token for more details.
Laravel 5: Ajax Post 500 (Internal Server Error) Error
Based on your comment, looks like you are using count()
on a non-array element, that's prohibited. You should modify your check:
From:
if (count($cart) > 0 ){
To:
if (is_array($cart) && count($cart) > 0 ){
Tip: Verify first that $cart
is an array before checking its length.
Laravel 5: Ajax Post 500 (Internal Server Error)
When you make a request via POST to a resource controller, it automatically calls store method:
Verb Path Action Route Name
----------------------------------
POST /articles store articles.store
So, you just need to change ajax url to:
$.ajax({
type: "POST",
url: 'http://localhost/laravel-5/public/articles',
When you need to send the session token, you can add a global meta-tag like this is you website:
<meta name="csrf-token" content="{{ csrf_token() }}">
Then, just add the token via ajax's headers:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
If you are using Form::open()
function (LaravelCollective) it adds a hidden input with the token as value with the name _token
. So, you can remove the meta-tag and edit your ajax's headers like this:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('[name="_token"]').val()
}
});
Laravel: Why my ajax request return 500 (Internal Server Error) ?
the server responds with 500 because some exceptions are raised during the ajax call. i guess its because of the code in controller, the controller code should be like this
if($request->ajax())
{
return "True request!";
}
Edit: to respond to ajax request, you can use response objects and http response codes, instead of returning plain text, so that you can know the exact status of ajax call,
Doc link to laravel responses
Link to Http Response Codes
Laravel AJAX POST Request failing (500 Internal Server Error
Stupid mistake from me here, but I guess I've never used AJAX before so wasn't aware this was an issue. I'm leaving this answer here just in case someone makes the same mistake.
Check storage/laravel.log to find out the error message when trying to run the function. In my case I had a couple of syntax errors in my likeCandidate function, and this was causing a 500 Internal Server Error (I assumed that the 500 error meant it wasn't able to access the controller.)
500 Internal Server Error when using Ajax in Laravel
you are calling the inserData statically. so it should be
public static function insertData($formData) {
DB::EnableQueryLog();
$sql = DB::table('form')->insert(['formKey' => 'testForm2', 'formData' => $formData]);
return $sql;
}
Related Topics
How to Correct This Illegal String Offset
Skip First Line of Fgetcsv Method
How to Split a String in PHP at the Nth Occurrence of a Needle
Wordpress Prepared Statement with In() Condition
Get Array's Key Recursively and Create Underscore Separated String
Laravel Route Issues with Route Order in Web.Php
Jquery Ajax Passing Value on PHP Same Page
Codeigniter, Result() VS. Result_Array()
How to Get Common Values from Two Different Arrays in PHP
Getting All Dates for Mondays and Tuesdays for the Next Year
Regex Matching Links Without <A> Tag
Split Alphanumeric String Between Leading Digits and Trailing Letters