我在项目中修改了Modal CRUD的AJAX脚本,因为我在解码一些提供的样本时遇到了麻烦。但是我在我的AJAX删除脚本中遇到了这个问题,控制台说:
POST http://localhost:8000/deleteItem 500(内部服务器错误) 发送@ app.js:10552 ajax @ app.js:10159 (匿名)@ dept_pagination:944 dispatch @ app.js:6192 elemData.handle @ app.js:6000
我注意到了#delete.dtem'不是我重定向的网址。在我的Ajax脚本中,我指出了deleteItem_dept'但执行后,它运行良好,但控制台抛出此错误,并将网址更改为' deleteItem'这是其他页面视图的URL。下面是我的Ajax脚本
<script type="text/javascript">
function dept_delete(id)
{
$('#footer_action_button_dept').text(" Yes");
$('#footer_action_button_dept').removeClass('glyphicon-check');
$('#footer_action_button_dept').addClass('glyphicon-trash');
$('.actionBtndept').removeClass('btn-success');
$('.actionBtndept').addClass('btn-info');
$('.actionBtndept').addClass('delete');
$('.id').text($(this).data('id'));
$('#deleteContentdept').show();
$('.form-horizontal').show();
$('#delModal').modal('show');
$('.modal-footer').on('click','#deldept', function(){
if (id == '')
{
alert('No department selected.')
}
else
{
$.ajax({
url: "/deleteItem_dept",
type: "post",
data: {
'_token': $('input[name=_token]').val(),
'id': id
},
success: function(data) {
document.getElementById(id).remove();
id = undefined; //reset ID to undefined
$('.form-horizontal').show();
}
});
}
});
$('.modal-footer').on('click','#candept', function(){
id = undefined; //reset ID to undefined
$('.form-horizontal').show();
});
}
</script>
控制器逻辑
public function deleteItem_dept(Request $request)
{
$data = $request->id;
DeptOffice::where('id', $data)->delete();
return response()->json();
}
答案 0 :(得分:0)
它应该在您的控制器中处理。首先检查对象是否存在
public function deleteItem_dept(Request $request) {
$data = $request->id;
if ($record = DeptOffice::where('id',$data)->first()) {
$record->delete()
}
return response()->json();
}