Reputation: 1633
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 viewTwo
loading 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 routeone
controller I redirect to this route with session data.
return redirect()->route('routeTwo')->with(['data'=>$data])
In the routeTwo
controller 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 viewTwo
loading 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.
Upvotes: 0
Views: 385
Reputation: 15125
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]);
Upvotes: 0