[英]Error status 500 Ajax on Laravel
很好,我嘗試通過Ajax刪除,但出現以下錯誤:
加載資源失敗:服務器響應狀態為500(內部服務器錯誤)
我搜索了該錯誤,並明顯地顯示了令牌,因此我完成了他們的建議,在此視圖中添加了此內容:
<meta name="csrf-token" content="{{ csrf_token() }}">
阿賈克斯:
$('#delete').on('click', function(){
var x = $(this);
var delete_url = x.attr('data-href')+'/'+x.attr('data-id');
$.ajax({
url: delete_url,
type:'DELETE',
headers:{
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr('content')
},
success:function(result){
alert('success');
},
error:function(result){
alert('error');
}
});
});
控制器:
public function destroy($id)
{
$appointment = Appointment::find($id);
if(appointment == null) {
return Response()->json([
'message'=>'error delete.'
]);
}
$appointment->delete();
return Response()->json([
'message'=>'sucess delete.'
]);
}
路線:
Route::name('appointments.destroy')->delete('/citas/{id}', 'AppointmentController@destroy');
這肯定是一個令牌錯誤,因為如果我在路線上不需要它,它將完美地完成...
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'citas/*'
];
}
您可以在開發者控制台的“網絡”標簽上檢查確切的錯誤。 在此處查找您的請求,然后檢查預覽或在新的Chrome瀏覽器標簽中打開預覽。
張貼的代碼有錯別字,也許它也在您的文件中? 您沒有將$
添加到appointment
變量中。
public function destroy($id)
{
$appointment = Appointment::find($id);
if($appointment == null) {
return Response()->json([
'message'=>'error delete.'
]);
}
$appointment->delete();
return Response()->json([
'message'=>'sucess delete.'
]);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.