我正在尝试使用ajax设置显示帖子的限制,一切似乎都很好,但是当我按下带有loadMore id的按钮时,我仍然看到相同的帖子。我不太了解自己在哪里犯了错误。有谁知道如何解决这个问题。我将不胜感激。
router.get("/blog", (req, res, next) => {
Post.find({})
.skip(0)
.limit(10)
.then(allPost => {
if (allPost) {
res.render("blog/index", {
Post: allPost,
moment: moment
});
} else {
return res.redirect("/");
}
})
.catch(err => {
console.error(err);
});
});
router.get("/api/blog", (req, res, next) => {
const page = req.query.page || 1;
const perPage = 10;
Post.find({})
.skip(perPage * page - perPage)
.limit(perPage)
.then(allPost => {
res.render("blog/posts", {
Post: allPost,
moment: moment
});
});
});
===============================
$(document).ready(function($) {
$("#loadMore").on("click", function(e) {
e.preventDefault();
e.stopPropagation();
console.log("click");
$.ajax({
type: "GET",
url: "/api/blog?page=2",
complete: function() {
console.log("process complete");
},
success: function(data) {
$("#posts").append(data);
},
errorinu: function() {
console.log("process error");
}
});
});
});