如何检查Jquery Ajax调用加载更多帖子功能的响应

发布于 2024-11-15 19:46:02 字数 909 浏览 0 评论 0原文

我正在尝试构建一个“加载更多”帖子功能,其中通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆悲凉 2024-11-22 19:46:02
function loadMore(pageNo) {
  var url = '/users?page=';
  $.get(url + pageNo, function(response) {
    if (response == "") {
        $("a.next").hide();
    }
    else {
        $("#users").append(response);
    }
  });
}
function loadMore(pageNo) {
  var url = '/users?page=';
  $.get(url + pageNo, function(response) {
    if (response == "") {
        $("a.next").hide();
    }
    else {
        $("#users").append(response);
    }
  });
}
花辞树 2024-11-22 19:46:02

有几种方法可以解决此问题,但 sudo 代码中的内容是正确的。根据您的“响应”的格式,您可以检查响应值。如果它只是纯文本或 html,您只需对照空字符串进行检查即可。

我个人会从服务器返回一个带有两个值的 JSON 对象,一个 has_more 标志和一个 data 标志,然后您可以像这样检查响应,

if(response.have_more)
{
    $("users").append(response.data);
}
else
{
    //hide more link
}

您可以使用类似以下内容在服务器上的 php 中构建 JSON 数组:

<?php
$query_results = getPosts(offset, amount);
if(count($query_results) > 0)
{
    $json['data'] = $query_results;
    $json['have_more'] = true;
}
else
{
    $json['have_more'] = false;
}
echo json_encode($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,

if(response.have_more)
{
    $("users").append(response.data);
}
else
{
    //hide more link
}

You can build the JSON array in php on the server with something like,

<?php
$query_results = getPosts(offset, amount);
if(count($query_results) > 0)
{
    $json['data'] = $query_results;
    $json['have_more'] = true;
}
else
{
    $json['have_more'] = false;
}
echo json_encode($json);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文