如何检查Jquery Ajax调用加载更多帖子功能的响应
我正在尝试构建一个“加载更多”帖子功能,其中通过 jquery 发出请求并添加更多帖子。基本上我正在使用这段代码:
function loadMore(pageNo) {
var url = '/users?page=';
$.get(url + pageNo, function(response) {
$("#users").append(response);
});
}
$(document).ready(function() {
var currPage = 1;
$("a.next").click(function() {
loadMore(++currPage);
});
});
一切都很完美,但我遇到的麻烦是如果没有更多的帖子要加载,则处理响应。
基本上,我需要检查响应,如果为空,则执行一些操作,例如隐藏加载更多链接,我知道该怎么做。我只需要帮助检查回复。我想象了一些类似的事情(一些伪代码),但无法弄清楚。没有jquery大师:(
function loadMore(pageNo) {
var url = '/users?page=';
$.get(url + pageNo, function(response) {
if response not blank
$("#users").append(response);
else
hide anchor or change text to "No More Posts"
end
});
}
即使没有更多的帖子,我的服务器也以200代码响应,如果没有更多的帖子要加载,我是否应该让服务器做出不同的响应?我想我只是不知道正确的处理没有更多帖子可加载的情况的方法,现在什么也没有发生,并且我的应用程序没有向用户提供任何类型的反馈,表明他们已加载所有可用的帖子
!
I am trying to build a "Load More" posts feature where a request is made via jquery and more posts are added. Basically I am using this code:
function loadMore(pageNo) {
var url = '/users?page=';
$.get(url + pageNo, function(response) {
$("#users").append(response);
});
}
$(document).ready(function() {
var currPage = 1;
$("a.next").click(function() {
loadMore(++currPage);
});
});
It all works perfectly, but the trouble I am having is handling the response if there are no more posts to load.
Basically, I need to check the response and if blank, perform some action such as hiding the load more link, which I know how to do. I just need help with checking the response. I imagine something along these lines (some psuedo code), but couldn't figure it out. No jquery guru :(
function loadMore(pageNo) {
var url = '/users?page=';
$.get(url + pageNo, function(response) {
if response not blank
$("#users").append(response);
else
hide anchor or change text to "No More Posts"
end
});
}
My server is responding with a 200 code even though there are no more posts, should I be having the server respond differently in the event there are no more posts to load? I guess I just don't know the proper way to handle the case where there are no more posts to load. Right now nothing happens and my app doesn't give the user any type of feedback indicating that they have loaded all the posts available.
Thanks a bunch!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有几种方法可以解决此问题,但 sudo 代码中的内容是正确的。根据您的“响应”的格式,您可以检查响应值。如果它只是纯文本或 html,您只需对照空字符串进行检查即可。
我个人会从服务器返回一个带有两个值的 JSON 对象,一个 has_more 标志和一个 data 标志,然后您可以像这样检查响应,
您可以使用类似以下内容在服务器上的 php 中构建 JSON 数组:
使用 JSON 对象这里可能看起来有点矫枉过正,但它是很好的做法,特别是如果您决定将来返回一些额外的数据。
There are a few ways to go about this but what you have in your sudo code is correct. Depending on how what format your "response" is you can check the response value. If it was just plain text or html you can just check it against an empty string.
I personally would return a JSON object from the server with two values, a have_more flag and a data flag and then you could check the response like so,
You can build the JSON array in php on the server with something like,
Using a JSON object may seem like overkill here but it makes for good practices, especially if you decide to return some additional data in the future.