1.添加测试按钮
<button class='test' >ajax测试</button>
2.ajax部分代码
@section('js')
<script type="text/javascript">
$('.test').on("click",function(){
//获取同一行其他字段的值 html()、text()、val()
var id = $(this).parents("tr").find(".id").text();
$.ajax({
type: 'POST',
url: '/admin/test',
data: { id : id, _token:"{{csrf_token()}}"},
dataType: 'json',
success: function(data){
//验证成功后实现跳转
window.location.href = "/admin/index";
},
error: function(xhr, status, error){
console.log(xhr);
console.log(status);
console.log(error);
}
});
});
</script>
@endsection
3.路由器Route::post('/admin/test', 'Admin\Controller@test');
4.控制器接收并处理数据
public function test(Request $request)
{
$testid=$_POST['id'];
if ($testid) {
return response()->json(array(
'status' => 1,
'msg' => 'ok',
));
} else {
return response()->json(array(
'status' => 2,
'msg' => 'fail',
));
}
}