前端代码:点击删除时,需要在a标签或者button标签中带数据的id
ajax代码:
$('#a_del').click(function(){
var url = "{:U('Article/del')}";
var id = $(this).attr('data');
$.ajax({
url:url,
data:{
id:id
},
type:'GET',
dataType:'json',
success:function(r){
if(r.status == 200){
alert(r.msg);
location=location;
}else{
alert(r.msg);
}
}
});
});
后端代码:
function del(){
$id = I('get.id');
// dump($id);die;
$atticle = M('Article');
if($atticle->delete($id)){
$data['status'] = 200;
$data['msg'] = '删除成功';
$this->ajaxReturn($data);
}else{
$data['status'] = 400;
$data['msg'] = '删除失败';
$this->ajaxReturn($data);
}
}