doucang8303 2018-10-11 11:09
浏览 132

当JS Ajax调用时,如何重定向到Laravel路由

I am trying to create a redirect to a route in laravel controller where the call to this controller came from an Ajax call in a js file.

Example: I loaded a view localhost:8000/viewone, in this view I run a js code that does an Ajax call to a Laravel route : routeone. In the controller function linked to routeone, the code does some data processing and then needs to load an other view : viewTwo.

The problem is that the viewTwoloading doesn't occur and all I get is the view HTML code in my return statement in onsuccess function of the Ajax call.

What I tried :

Create a controller inked to viewTwo by creating a new route routeTwo, in my routeonecontroller I redirect to this route with session data.

return redirect()->route('routeTwo')->with(['data'=>$data])

In the routeTwocontroller I get the session data and return the viewTwo.

$data = \Session::get('data');
return view('viewTwo',['data'=>$data]);

It still doesn't work. What happens is that viewTwoloading happens as an XHR request and I get the HTML page which would have been loaded in the main browser view ( like the first view ) as XHR response data.

Question : How to force redirect to/load view when call came from Js AJAX function.

  • 写回答

1条回答 默认 最新

  • dongshen4129 2018-10-11 11:28
    关注

    by load or render html from ajax do like this in first view page

    <div class="newView"></div>
    

    by ajax success function add this line

    $('.newView').html(data.html);
    

    in controller

    $view = view("viewnamewhatuhave",compact('data'))->render();
    

    by view facade

    $view =  View::make('viewnamewhatuhaveset',[$data])->render();
    
    return response()->json(['html' => $view]);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100