0

I'm trying to use this function for infinite scroll:

<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>

The problem is it's not appending the data to the unordered list, even though http://localhost:3000/paginate?format=js&page=2 works perfectly.

If I put console.log beneath the line that says: if(nearBottomOfPage()) { it triggers, so i know the function is working fine. Yet if I put the console.log within success: for the ajax function it never triggers.... and still further, If i run the ajax command through console it returns exactly what I need in the 'responseText', with success code 200... so i have no idea why it would nto be triggering a success message in console log if its successfully returning 200 when i do it manually..

heres the controller

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

    respond_to do |format|
      format.js
    end
  end
8
  • 1
    Have you tried using $.getScript() instead of $.ajax()? Commented Apr 16, 2012 at 3:53
  • this is more a ruby question, but its 400 (Bad Request) because its forming the url like this... localhost:3000/[object%20Object]?_=1334548636070 Commented Apr 16, 2012 at 3:58
  • Use a debugger to figure out why the URL is wonky. I don't know where that URL is coming from, but I do know that '[object Object]' comes from toString()ing an object; for example: ({}).toString() Commented Apr 16, 2012 at 4:00
  • the URL is hardcoded into the script (see above), its just /paginate?page=x. As far as whats on the page, it must be in script format (format.js in respond_to method) and you will see plaintext html for a bunch of list items Commented Apr 16, 2012 at 4:03
  • 1
    have tried with dataType: 'text' ? Commented Apr 16, 2012 at 5:47

1 Answer 1

0

I used the following code:

$.ajax({ 
  url: '/paginate?format=js&page=' + page, 
  type: 'get', 
  dataType: 'text', 
  success: function(data) { 
    $(".res-list").append(data); 
    loading = false; 
  } 
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.