Laravel return view ajax request example


Sometime, we may require to return view from controller via jquery ajax request in Laravel 5 application. So in this example i will let you know how to generate view from controller and return it.

Here, i will create two blade file and another controller method so you have to just follow bellow example. Here bellow blade file.

blade file:

<!DOCTYPE html>

<html>

<head>

<title>Laravel - Ajax Request</title>

<script src="http://demo.itsolutionstuff.com/plugin/jquery.js"></script>

</head>

<body>

<div class="container">

</div>

<script type="text/javascript">

$.ajax(

{

url: "/getBody",

type: 'GET',

}).done(

function(data)

{

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

}

);

</script>

</body>

</html>

controller method:

public function getBody()

{

$title = "HDTuto.com";

$view = view("ajaxView",compact('title'))->render();

return response()->json(['html'=>$view]);

}

ajaxView.blade.php file:

<h1>{{ $title }}</h1>

</p>

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod

tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,

quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo

consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse

cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non

proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

</p>

I hope you find your best solution....


August 8, 2017 | 3560 Viewer | Category : PHP Laravel JQuery Ajax

Related Posts: