Roy Sijnesael Roy Sijnesael - 2 months ago 43
Ajax Question

Laravel 5.3, Ajax post 500 error

I've been trying to get my POST to work but nothing is really working. I got my UPDATE and DELETE to work, but I'm still getting 500 errors when I'm trying to post something.

I already have my CSRF-TOKEN on top of my file.

//create new task / update existing task
$("#btn-save").click(function (e) {
e.preventDefault();

var formData = {
task: $('#task').val(),
description: $('#description').val(),
}

//used to determine the http verb to use [add=POST], [update=PUT]
var state = $('#btn-save').val();

var type = "POST"; //for creating new resource
var task_id = $('#task_id').val();;
var my_url = url;

if (state == "update"){
type = "PUT"; //for updating existing resource
my_url += '/' + task_id;
}

console.log(formData);

$.ajax({
type: type,
url: my_url,
data: formData,
dataType: 'json',
success: function (data){
console.log(data);

var task = '<tr id="task' + data.id + '"><td>' + data.task + '</td><td>' + data.description + '</td><td>' + data.created_at + '</td><td>' + data.done + '</td>';
task += '<td><button class="btn btn-warning btn-xs btn-detail open-modal" value="' + data.id + '">Bewerk</button>';
task += '<button class="btn btn-danger btn-xs btn-delete delete-task" value="' + data.id + '">Verwijder</button></td></tr>';

if (state == "add"){ //if user added a new record
$('#tasks-list').append(task);
}else{ //if user updated an existing record

$("#task" + task_id).replaceWith( task );
}

$('#frmTasks').trigger("reset");

$('#myModal').modal('hide')
},
error: function (data) {
console.log('Error:', data);
}
});
});
});

Answer

500 means its an internal server error, so something in your php is wrong.

Open your storage/logs/laravel.log and go to your last error. That gives you a clue what the problem is.

(If you have APP_DEBUG=true in your .env, then you should also be able to inspect the error response in the browser.)