dongqi8863 2013-12-31 01:03
浏览 38
已采纳

将内容类型更改为JSON时,从数据库加载更多内容不起作用

i am trying to stop my 'load more' button from displaying once there is no more results to show.

So, my plan was that once the load more button was clicked and the query ran on the database, i would do a rowcount and then if that number was less than what was requested in the LIMIT claus then i would return that information.

So currently my js script looks like this:

$(document).ready(function(){
    var pageIndex = 1;
    $('#loadmorebuilds-div').click(function() {
        $('#buildcontainer').imagesLoaded( function(){
        $.ajax({
           url: 'includes/loadmorebuilds.php?type=follow&pageIndex=' + pageIndex,
           success: function(html) {
              var el = jQuery(html);
              jQuery("#buildcontainer").append(el).masonry( 'reload' );
              $("#loadmorebuilds-div").stop();
              pageIndex++;
              $("#buildcontainer").masonry()
           }

        });

    });

});
});

This works fine, but i wanted to put a JSON return in my loadmorebuilds.php page.

So the plan was to do e.g.

// check if the results count is less than requested
if ($checkforlast < 8) {
    $return['noMore'] = true;
    echo json_encode($return);
}

I would then change my js to look like so:

$(document).ready(function(){
    var pageIndex = 1;
    $('#loadmorebuilds-div').click(function() {
        $('#buildcontainer').imagesLoaded( function(){
        $.ajax({
           url: 'includes/loadmorebuilds.php?type=follow&pageIndex=' + pageIndex,
           success: function(html) {
              var el = jQuery(html);
              jQuery("#buildcontainer").append(el).masonry( 'reload' );
              $("#loadmorebuilds-div").stop().fadeOut();
              pageIndex++;
              $("#buildcontainer").masonry()

              if (response.correctKey === true) {
                DO SOMETHING TO REMOVE BUTTON
              }

           }

        });

    });

});
});

The problem comes when i change the content type:

header('Content-Type: application/json');

Because thats changing how the data i send back is (i think?). Changing the content type results in no new content being shown and nothing happens on the load more button being clicked.

is there another way to send the databack to the ajax call if there is less results than requested and without changing the content type?

Hope this makes sense!

  • 写回答

2条回答 默认 最新

  • duanju6788 2013-12-31 02:03
    关注

    I agree with @palpatim that string parsing is probably not the best way to go here. But this solution could work as well, and wouldnt require you building your html from json on the client:

    $(document).ready(function(){
      var pageIndex = 1;
      $('#loadmorebuilds-div').click(function() {
        $('#buildcontainer').imagesLoaded( function(){
          $.ajax({
            url: 'includes/loadmorebuilds.php?type=follow&pageIndex=' + pageIndex,
            success: function(html) {
              var el = jQuery(html),
                  rowCount = el.find('tr').length;
    
              jQuery("#buildcontainer").append(el).masonry( 'reload' );
              $("#loadmorebuilds-div").stop().fadeOut();
              pageIndex++;
              $("#buildcontainer").masonry();
    
              if (rowCount < 8) {
                DO SOMETHING TO REMOVE BUTTON
              }
           }
        });
    });
    });
    

    I used 'tr' here to denote the element you're using to hold each result in markup.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突