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

来源:互联网 发布:手机wifi网络看视频卡 编辑:程序博客网 时间:2024/06/08 18:39

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',                                            ));    }}




阅读全文
0 0
原创粉丝点击