Laravel通過ajax的POST方式傳值並實現頁面跳轉
阿新 • • 發佈:2019-01-10
1.新增測試按鈕
<button class='test' >ajax測試</button>
2.ajax部分程式碼
3.路由器@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
Route::post('/admin/test', 'Admin\[email protected]');
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', )); } }