我们在laravel模板视图中可能经常会用到ajax异步处理页面上的交互,比如下面的情况.
laravel的blade模板中如下:
删除
其它页面中的javascript方法如下:
//删除分类 function categoryDel(id){ layer.confirm( '您确定要删除这个分类吗?', {btn:['确定','取消']}, function(){ $.post( "{ {url('admin/category')}}/"+id, { '_token':'{ {csrf_token()}}', '_method':'delete', 'cate_id':id, }, function(data){ if(data.status==200){ location.href = location.href; layer.msg(data.msg, {icon: 6}); }else{ layer.msg(data.msg, {icon: 5}); } } ); }, function(){} ); }
laravel控制器方法如下:
/** * 删除分类 * DELETE /category/{category} */ public function destroy($id){ if (preg_match("/^[0-9]\d*$/", $id) == FALSE) { $data = array( 'status'=>500, 'msg'=>'参数错误[分类ID不正确]', ); return $data; } $rows = Category::where('cate_pid',$id)->get(); if($rows->all()){ $data = array( 'status'=>500, 'msg'=>'请先删除其它分类下的子分类再试!', ); return $data; } $ret = Category::where('cate_id',$id)->delete(); if($ret){ $data = array( 'status'=>200, 'msg'=>'删除分类成功!', ); }else{ $data = array( 'status'=>500, 'msg'=>'删除分类失败,请稍后重试!', ); } return $data; }