您的位置:首页 > 编程语言 > PHP开发

Laravel通过ajax的POST方式传值并实现页面跳转

2017-10-18 09:16 375 查看
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',
));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: