如果有控制器,該指數函數:laravel 4 PAGINATE get函數返回JSON
public function getIndex() {
return View::make('web.index')
->with('projects', Project::orderBy('id', 'DESC')->paginate(8));
}
在我看來有foreach循環
@foreach($projects as $project)
<a href="like/{{$project->id}}" class="action like {{$liked}}">
{{$project->likes}}
</a>
@endforeach
<div class="pagination">
{{$projects->links()}}
</div>
的getLike功能:
public function getLike($id) {
if(Cookie::get('like_'.$id) != 'true')
{
$project = Project::find($id);
$project->increment('likes');
$cookie = Cookie::forever('like_'.$id, 'true');
$s = array(
'status' => 'success'
);
$response = Response::json($s);
$response->headers->setCookie($cookie);
return $response;
}
$response = array(
'status' => 'error'
);
return Response::json($response);
}
阿賈克斯請求:
$(".like").on("click", function(){
$.get(
$(this).prop('href'),
{
},
function(data) {
if(data['status'] == 'error')
{
alert("Already Liked");
}
else {
alert('Like');
}
},
'json'
);
return false;
});
getLike()函數完美地工作!(使用ajax來收集數據),但是當頁面2重新加載時(使用無限滾動),getLike函數返回帶有json文本的空白頁面。 幫助...
您是否啓用了錯誤reporing並顯示錯誤? AJAX請求(瀏覽器控制檯)返回的內容是什麼?檢查你的ajax請求的響應狀態。 – Rolice
@Rolice是在我的app.php中設置'debug'=> true,這是Ajax請求: $(「。like」)。on(「click」,function(){ $ .get( $ (本).prop( 'href' 屬性), { }, 功能(數據){ 如果(數據[ '狀態'] == '錯誤') { 警報( 「已喜歡」); } 其他{ 警報( '喜歡');} } , 'JSON' ); 返回FALSE; }); – user3519234
和響應狀態? – Rolice