have you looked in the laravel log file?
Laravel Ajax - Put request 500 internal server error
Hi All,
It has been almost a week and still I'm not able to find out the 500 internal server error mystery which is happening only on when the application is on running on a shared server (works perfectly on local machine).
Summary of what i am trying: After creating a Ticket, add n-number of TicketNotes to the Ticket while editing the Ticket. In order to add the TicketNotes I am using ajax CRUD for the TicketNotes. BTW I am using Bootstrap 3 Modal.
I am able to update Ticket but when I try to update TicketNotes it fails on shared hosting, throws this error in console.log. And the best part is GET POST and DELETE all are working only on PUT it's throwing error. Its so frustrating... NEED HELP!!!
Here is my code:
app.blade.php
. . .
. . .
<meta name="csrf-token" content="{{ csrf_token() }}">
. . .
. . .
Ticket View with editTicket.blade.php [TicketNotes - Table]
<div class="form-group">
<label for="note_description">@lang('description-od-additional-request')</label>
<button type="button" class="btn btn-default btn-embossed btn-sm pull-right" data-toggle="modal" data-target="#addNoteModal"><span class="fui-plus"></span> @lang('add-new')</button>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th valign="middle">@lang('hash')</th>
<th>@lang('note-description')</th>
<th>@lang('add-deliverables')</th>
<th>@lang('created')</th>
<th>@lang('actions')</th>
</tr>
</thead>
<tbody id="ticketNotesTable">
@foreach($ticketNotes as $noteIndex => $ticketNote)
<tr id="ticketNote{{$ticketNote->TicketNoteID}}">
<td class="noteCol1">{{++$noteIndex}}</td>
<td>{!! $ticketNote->NoteDescription !!}</td>
<td>{!! $ticketNote->AddDeliverables !!}</td>
<td>{{ $ticketNote->created_at->diffForHumans() }}</td>
<td>
<div class="btn-group" role="group" aria-label="Ticket Notes">
<button type="button" class="editNoteModal btn btn-link" data-noteid="{{$ticketNote->TicketNoteID}}"><i class="fui-new"></i></button>
<button type="button" class="deleteNoteModal btn btn-link" data-noteid="{{$ticketNote->TicketNoteID}}"><i class="fui-trash text-danger"></i></button>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
TicketNotes Modal View [tickets/ticketnotes/edit.blade.php]
<div class="modal fade" id="editNoteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">@lang('edit-additional-request')</h4>
</div>
<div class="modal-body">
<form id="editNoteForm">
<input type="hidden" id="edit_ticketNote_id">
<div class="form-group">
<label for="edit_note_description">@lang('description-od-additional-request')</label>
<textarea class="form-control" id="edit_note_description"></textarea>
</div>
<div class="form-group">
<label for="edit_additional_deliverables">@lang('additional-deliverables')</label>
<textarea class="form-control" id="edit_additional_deliverables"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">@lang('close')</button>
<button type="button" class="btn btn-primary editNotes">@lang('update')</button>
</div>
</div>
</div>
</div>
Ajax Script [edit method]
// Ajax Setup
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
// Edit a ticket note
$(document).on('click', '.editNoteModal', function () {
var noteId = $(this).data('noteid')
$.ajax({
type: "GET",
url: '/ticketNotes/'+ noteId,
success: function (data) {
console.log(data);
$('#edit_ticketNote_id').val(data.TicketNoteID);
$('#edit_note_description').val(data.NoteDescription);
$('#edit_additional_deliverables').val(data.AddDeliverables);
$('#editNoteModal').modal('show');
},
error: function (data) {
console.log('Error:', data);
}
});
});
$('#editNoteModal').on('click', '.editNotes', function () {
var editNotesFormData = {
edit_note_description: $('#edit_note_description').val(),
edit_additional_deliverables: $('#edit_additional_deliverables').val()
}
$.ajax({
type: 'PUT',
url: '/ticketNotes/'+ $('#edit_ticketNote_id').val(),
data: editNotesFormData,
dataType: 'json',
success: function (data) {
console.log('Success:', data);
var editTicketNote = '<tr id="ticketNote' + data.TicketNoteID + '">'
editTicketNote += '<td class="noteCol1"></td>'
editTicketNote += '<td>' + data.NoteDescription + '</td>'
editTicketNote += '<td>' + data.AddDeliverables + '</td>'
editTicketNote += '<td>Just now!</td>'
editTicketNote += '<td><button type="button" class="editNoteModal btn btn-link" data-noteid="' + data.TicketNoteID + '"><i class="fui-new"></i></button>'
editTicketNote += '<button type="button" class="deleteNoteModal btn btn-link" data-noteid="' + data.TicketNoteID + '"><i class="fui-trash text-danger"></button></td><tr>';
toastr.success('Successfully updated Additional Request!', 'Success Alert', {timeOut: 3000});
$('#ticketNote' + data.TicketNoteID).replaceWith(editTicketNote);
$('.noteCol1').each(function (editIndexNotes) {
$(this).html(++editIndexNotes);
});
$('#editNoteForm').trigger('reset');
$('#editNoteModal').modal('hide');
},
error: function (data) {
console.log('Error:', data);
}
});
});
Controller [update method]
public function update(Request $request, $id)
{
$ticketNote = TicketNote::findOrFail($id);
$ticketNote->NoteDescription = $request->edit_note_description;
$ticketNote->AddDeliverables = $request->edit_additional_deliverables;
$ticketNote->save();
return response()->json($ticketNote);
}
THANKS!!!
If you try changing to post method, also add {_method: 'PUT'} to the data being sent.
What does the response tab look like in your browsers dev tools for your ajax request? Usually it shows the stack trace.
Please or to participate in this conversation.