我在页面上有一个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