I'm trying to update my DB with AJAX once there are some changes, but I seem to miss something.
My controller:
// Update dates
public function updateDates(Request $request)
{
// dd($input);
$event = new Event();
$event->invoice = $request->input('invoice');
$event->paid = $request->input('paid');
$event->save();
return response()->json(['success'=>'Got Simple Ajax Request.']);
}
Routes:
//Events
Route::resource('event', 'EventController');
Route::post('/updateDates', ['as' => 'updateDates', 'uses' => 'EventController@updateDates']);
Jquery:
$('.datepickerstart').datepicker({
autoclose: true,
todayHighlight: true,
format: 'yyyy-mm-dd'
}).on('changeDate', function(e) {
e.preventDefault();
var update_id = $(this).data('evid');
var invoice = $("input[name=invoice_date]").val();
var payment = $("input[name=payment_date]").val();
console.log(update_id, invoice, payment);
$.ajax({
method:'post',
url:'{{ route("updateDates") }}',
dataType:'JSON',
data: {
id : update_id,
invoice : invoice,
paid : payment
},
success: function(data) {
alert(data.success);
}
});
});
I do console log correct data before ajax post. Cannot even dd() request in my controller, what am I doing wrong?
Console error:
VM4649:1 POST http://localhost:8000/updateDates 500 (Internal Server Error)
I was getting "undefined" erlier.
UPDATE: solved, after looking through the logs. I was creating new objects instead of updating them and got error: no default field value
Add csrf field in your data post like this :
data: {
id : update_id,
invoice : invoice,
paid : payment,
_token: '{{csrf_token()}}'
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.