Check the laravel log file
Laravel Ajax 500 Internal Error
I have gone through many threads about this error, but none helped my case. I have implemented AJAX Post from the blade view to store some data in a table. But the execution of the code stops at $.ajax(), showing 500 Internal Error for Post. Using Dev Tools, the Laravel side error shown was, Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException The GET method is not supported for this route. Supported methods: POST. for the URL in the POST Request. Here are the snippets for reference, blade view:
<meta name="csrf-token" content="{{ csrf_token() }}">
<script>
$(document).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var events = @json($events);
$('#calendar').fullCalendar({
$('#saveBtn').click(function() {
var title = $('#title').val();
var start = moment(start).format('YYYY-MM-DD');
var end = moment(end).format('YYYY-MM-DD');
$.ajax({
url: "{{ route('calendar.store') }}",
type: 'post',
dataType: 'json',
data:{title: title, start: start, end: end, type: 'add'},
success:function(response)
{
$('#addEventModal').modal('hide')
$('#calendar').fullCalendar('renderEvent', {
'title': response.title,
'start' : response.start,
'end' : response.end
});
},
error:function(error)
{
if(error.responseJSON.errors) {
$('#titleError').html(error.responseJSON.errors.title);
}
},
})
});
});
});
</script>
Controller:
public function store(Request $request)
{
$request->validate([
'title' => 'required|string'
]);
$event = Events::create([
'title' => $request->title,
'start_date' => $request->start_date,
'end_date' => $request->end_date,
]);
return response()->json([
'id' => $event->id,
'start' => $event->start_date,
'end' => $event->end_date,
'title' => $event->title,
]);
}
routes.php:
Route::get('calendar/index', [App\Http\Controllers\CalendarController::class, 'index'])->name('calendar.index');
Route::post('calendar', [App\Http\Controllers\CalendarController::class, 'store'])->name('calendar.store');
Though the URL has a post route, I am facing this error. I have also tried sending the token as _token through the data array in AJAX Post, but the issue persists. I did not find any info in the log as well. I tried multiple solutions but could not get this part to work.
@Reenter05 what can you see in the Browser console and/or the Network request/response devtools?
Please or to participate in this conversation.