我想为我的博客使用无限的ajax滚动分页,但不幸的是我没有结果!?我仍然收到此错误消息:服务器没有响应... 在使用的代码下面。
控制器:
$posts =Post::paginate(5);
if ($request->ajax()) {
$view = view('user.list.data', compact('posts'))->render();
return response()->json(['html'=>$view]);
}
return view('user.list.page', compact('posts');
视图:
<div class="container">
<h2 class="text-center">Liste poste</h2>
<br/>
<div class="col-md-12" id="post-data">
@include('user.list.data')
</div>
</div>
<div class="ajax-load text-center" style="display:none">
<i class="fa fa-spinner fa-spin" style="color: #dd4b39; font-size:36px"></i>
</div>
<script type="text/javascript">
var page = 1;
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() >= $(document).height()) {
page++;
loadMoreData(page);
}
});
function loadMoreData(page) {
$.ajax({
url: '?page=' + page,
type: "get",
beforeSend: function() {
$('.ajax-load').show();
}
}).done(function(data) {
if(data.html == " ") {
$('.ajax-load').html("No more records found");
return;
}
$('.ajax-load').hide();
$("#post-data").append(data.html);
}).fail(function(jqXHR, ajaxOptions, thrownError) {
alert('server not responding...');
});
}
</script>
查看数据:
@foreach($posts as $post)
<div>
<h3>
<a href="">{{ $post->title }}</a>
</h3>
<p>{{ $post->description }}</p>
<div class="text-right">
<button class="btn btn-success">Read More</button>
</div>
<hr style="margin-top:5px;">
</div>
@endforeach