简体   繁体   English

通过ajax获取分页页面的内容,对其进行过滤并将其附加到当前页面

[英]Get content of paginated pages via ajax, filter it and append it to current page

I would like to load more functionality on my blog. 我想在博客上加载更多功能。 I now have paginated pages, which is not what I want. 我现在有分页页面,这不是我想要的。 I thought it would be easy to get the contents of these paginated pages via ajax, filter it, and append it to a div. 我认为通过ajax获取这些分页页面的内容,对其进行过滤并将其附加到div会很容易。

I'm not really sure if this is the way to go. 我不确定这是否可行。 This is what I tried: 这是我尝试的:

$("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);
      }
    });
  });

Can you guys point me in the right direction? 你们能指出我正确的方向吗?

Your solution would work, providing that you'd detect the ajax call in the back end, and instead of the entire page, return just the parts where you are interested in (use Director::is_ajax() for that, and follow the hints in the comment of @micmania). 您的解决方案将起作用,前提是您要在后端检测到ajax调用,而不是整个页面,仅返回您感兴趣的部分(为此使用Director :: is_ajax(),并按照提示进行操作即可)在@micmania的评论中)。 If you would rather not make your controllers more complex and you don't mind the extra data being sent, you could also just retrieve the entire page (as you are doing now), and pick just the parts that you want to append to your existing document: 如果您不想让控制器变得更复杂并且不介意发送额外的数据,则也可以只检索整个页面(就像现在所做的那样),然后只选择要添加到页面上的部分。现有文件:

$.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);
  }
});

This solution also degrades nicely for people not using javascript, although I'm not exactlye sure if that's of any added value these days :-) 对于不使用javascript的人来说,该解决方案的效果也很好,尽管我不确定这些天是否具有任何附加价值:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM