I'm trying to toggle the state on and off with the toggle (checkbox) with ajax but I'm getting error 500 (internal server error)
and I have no idea why. I already put the token but the error is still there. What else can I do?
- Controller
public function updateStatus(Request $request)
{
$profesor = Profesor::findOrFail($request->id)->update(['calificar' => $request->calificar]);
if($request->calificar == 0) {
$newStatus = '<br> <button type="button" class="btn btn-sm btn-danger">Inactiva</button>';
}else{
$newStatus ='<br> <button type="button" class="btn btn-sm btn-success">Activa</button>';
}
return response()->json(['var'=>''.$newStatus.'']);
}
- View
<meta name="csrf-token" content="">
<label class="switch">
<input data-id="" class="mi_checkbox" type="checkbox" data-token=""
data-onstyle="success" data-offstyle="danger" data-toggle="toggle" data-on="Active"
data-off="InActive" >
<span class="slider round"></span>
</label>
-ajax
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.mi_checkbox').change(function() {
//Verifico el estado del checkbox, si esta seleccionado sera igual a 1 de lo contrario sera igual a 0
var estatus = $(this).prop('checked') == true ? 1 : 0;
var id = $(this).data('id');
console.log(estatus);
$.ajax({
type: "GET",
dataType: "json",
url: 'StatusProfesor',
data: {'calificar': estatus, 'id': id},
success: function(data){
$('#resp' + id).html(data.var);
console.log(data.var)
}
});
});
In the console the values are changing fine, from 1 to 0, but this error still appears and nothing is updated in the database.
Aucun commentaire:
Enregistrer un commentaire