我在頁面上有一個ajax帖子加載器。調用它的鏈接看起來是這樣的:ajax頁面加載器404錯誤
<div class="load_more_cont">
<p align="center">
<a href="http://www.example.com/category/books/page/5/" title="Load more"><span class="fa fa-angle-down fa-fw button" aria-hidden="true"></span></a>
</p>
</div>
,這是AJAX:
$('.load_more_cont a').on('click', function(e)
{
e.preventDefault();
//how many posts per page
var posts_per_page = 12;
//set button to contain spinning loading icon
$(this).html('<span class="fa fa-refresh fa-spin fa-fw loading" aria-hidden="true">');
console.log($(this).attr('href') + '#main_container');
$.ajax({
type: "GET",
url: $(this).attr('href') + '#main_container',
dataType: "html",
success: function(out)
{
//result of query
result = $(out).find('#load_posts_container .home_post_box');
//append result to posts container
$('#load_posts_container').append(result).masonry('appended', result, true);
//find the next link in the returned result
nextlink = $(out).find('.load_more_cont a').attr('href');
//if (nextlink != undefined)
if (result['length'] == posts_per_page)
{
//add the next link to the load more button
$('.load_more_cont a').attr('href', nextlink);
$('.load_more_cont a').html('<span class="fa fa-angle-down fa-fw button" aria-hidden="true">');
}
else
{
//remove the load more button and add a clearing div
$('.load_more_cont').remove();
$('#load_posts_container').append('<div class="clear"></div>');
}
},
error: function(xhr, ajaxOptions, thrownError){
console.log(xhr.status);
console.log(thrownError);
}
});
});
它工作的很好,但是當我到達的類別5頁(如圖書5頁以上)我收到一個404錯誤。如果我在瀏覽器中加載類別url沒有問題 - 第5頁和第6頁都存在並加載。一切都在同一臺服務器上。如果我不選擇一個類別,所以url看起來像這樣http://www.example.com/page/5/
那麼它也可以正常工作。任何想法可能是什麼問題?
您是否檢查過$(this).attr('href')'以確保它返回正確的路徑?您可以通過console.log($(this).attr('href'));'在控制檯中進行打印,或者您可以使用開發工具中的網絡部分監控ajax調用。 – NewToJS
這就是我發佈的代碼。 – garrettlynch
它是否顯示正確的路徑?你有沒有在開發工具中檢查網絡?它必須是一個錯別字,因爲404意味着它不存在。嘗試從通話中刪除'#main_container'。 – NewToJS