好我试图通过ajax删除但是我收到以下错误:
无法加载资源:服务器响应状态为500(内部服务器错误)
我搜索了错误并且显然是由令牌显示,所以我已经完成了他们推荐的内容,我在视图中添加了:
<meta name="csrf-token" content="{{ csrf_token() }}">
AJAX:
$('#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/*'
];
}
答案 0 :(得分:0)
您可以在开发者控制台,网络标签上查看确切的错误。查看您的请求并查看预览或在新的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.'
]);
}