我想在我的博客上加载更多功能。我现在有分页页面,这不是我想要的。我认为通过ajax获取这些分页页面的内容很容易,过滤它并将其附加到div。
我不确定这是否可行。这就是我试过的:
$("a.next").on("click", function(e){
e.preventDefault();
var nextLink = $(this).attr("href");
console.log(nextLink);
$.ajax ({
url: "http://localhost:8888/" + nextLink,
datatype: 'html',
type: "GET",
success: function(data) {
var response = data;
$(".blogItems .holder").append(response);
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
});
你们能指出我正确的方向吗?
答案 0 :(得分:1)
您的解决方案可行,只要您在后端检测到ajax调用,而不是整个页面,只返回您感兴趣的部分(使用Director :: is_ajax()为此,并按照@micmania评论中的提示进行操作。如果您不想让控制器变得更复杂并且您不介意发送额外的数据,您也可以只检索整个页面(就像您现在所做的那样),并选择您想要追加的部分到您现有的文件:
$.ajax ({
url: "http://localhost:8888/" + nextLink,
datatype: 'html',
type: "GET",
success: function(data) {
var response= $("#theDivIWant", data); //<- Here (untested, but should do the trick
$(".blogItems .holder").append(response);
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
对于不使用javascript的人来说,这个解决方案也会很好地降级,尽管我现在还不确定这些是否有任何增值: - )