How to Return a View from an Ajax Call in Laravel 5

Laravel return a view in an Ajax response

They are not working for you because they return a JSON response and not an html one. You should return the view directly, without the render method call.

Try this

return view('pages.your_page');

or

return view('pages.your_page')->render();

Laravel 5.2 - Return view from controller after ajax request

SOLVED

Not the most elegant solution but what I did was set the array with errors as an session variable and get that session var in the specific controller I need.

Controller

   $request->session()->put('importErrors',$erro);
$response = array('status' =>'success','url' => '/ListarContactos/'.$idList);
return response()->json($response);

JavaScript

$('#importar').submit(function(e) {
e.preventDefault();
fdata=preparePostData();
$.ajax({
type:'POST',
url: $(this).prop('action'), // url, from form
data:fdata,
processData: false,
contentType: false,
success:function(data) {
if(data.status=='success'){
window.location.replace(data.url);
}

}
});
}); // end form.submit

XPTOController

$errorArray= $request->session()->get('importErrors');

After using it you can destroy the session variable or keep it(depending if you need it or not).



Related Topics



Leave a reply



Submit