I am A PHP developer and currently moving towards Laravel framework as per my task I have to complete the real-time table using ajax but I am sticking with an error which is CSRF token mismatch error please help me to resolve the error I am posting shortcode only
ajax
$(document).ready(function(){
$('.toggle-class').change(function() {
var status = $(this).prop('checked') === true;
var id = $(this).data('id');
var csrf=$('meta[name="csrf-token"]').attr('content');
$.ajax({
type: "POST",
dataType: "json",
url: '/package/status',
data: {'status': status, 'id': id, 'XSRF-TOKEN': csrf},
success: function(data){
console.log('success '+data);
}
});
})
})
Route Path
Route::post('package/status', [App\Http\Controllers\PackageController::class,'status']);
Controller code
public function status(Request $request){
$Package = package::find($request->id);
$Package->status = $request->status;
$Package->save();
return response()->json(['success'=>'Status change successfully.']);
}
I don't know why it is showing me CSRF token mismatch when headers contain tokens once solved I can have some real-time action.
Try this:
data: {'status': status, 'id': id, '_token': '{{csrf_token()}}'},