0

我正在尝试将此功能用于无限滚动:

<script type="text/javascript" charset="utf-8">
(function() {
  var page = 1,
      loading = false;

  function nearBottomOfPage() {
    return $(window).scrollTop() > $(document).height() - $(window).height() - 200;
  }

  $(window).scroll(function(){
    if (loading) {
      return;
    }

    if(nearBottomOfPage()) {
      loading = true;
      page++;
      $.ajax({
        url: '/paginate?page=' + page,
        type: 'get',
        dataType: 'script',
        success: function(data) {
          $(".res-list").append(data.responseText);
          $(window).sausage('draw');
          loading = false;
        }
      });
    }
  });

  $(window).sausage();
}());
</script>

问题是它没有将数据附加到无序列表中,即使http://localhost:3000/paginate?format=js&page=2效果很好。

如果我将 console.log 放在下面写着:if(nearBottomOfPage()) {它会触发,所以我知道该功能工作正常。但是,如果我将 console.log 放入成功:对于 ajax 函数,它永远不会触发....更进一步,如果我通过控制台运行 ajax 命令,它会返回我在“responseText”中需要的内容,成功代码为 200 ...所以我不知道为什么它会在我手动执行时成功返回 200 时在控制台日志中触发成功消息。

这是控制器

  def paginate
    @resources = Resource.order(:created_at).page(params[:page]).per(20)

    respond_to do |format|
      format.js
    end
  end
4

1 回答 1

0

我使用了以下代码:

$.ajax({ 
  url: '/paginate?format=js&page=' + page, 
  type: 'get', 
  dataType: 'text', 
  success: function(data) { 
    $(".res-list").append(data); 
    loading = false; 
  } 
});
于 2012-04-30T15:07:13.270 回答