如何输出json

时间:2015-10-07 19:12:11

标签: php json ajax

我有这个PDO声明:

  public static function ajax($start = 0){
    $limit = 4;
        $database = DatabaseFactory::getFactory()->getConnection();

    $sql = 'SELECT * FROM MOCK_DATA ORDER BY date DESC LIMIT :start, :limit';
    $query = $database->prepare($sql);
    $query->bindValue(':start', intval($start), PDO::PARAM_INT);
    $query->bindValue(':limit', intval($limit), PDO::PARAM_INT);
    $query->execute();

        if($query->rowCount() < 0){
            return false; 
        } 

        $rowcount = $query->rowCount();
        $data = new StdClass();
        $data->count = $rowcount;
        $data->content = $query->fetchAll();

        return json_encode($data);
    }

(注意我在stdClass中添加了一个额外的列(?),count

返回这个json:

   {"mockup":"{\"count\":4,\"content\":
[{\"id\":\"408\",\"email\":\"jbrooksbb@theglobeandmail.com\",\"ip_address\":\"128.42.226.235\",\"date\":\"2015-10-05\",\"text\":\"Cras in purus eu magna vulputate luctus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.\"},
{\"id\":\"832\",\"email\":\"wlanen3@ning.com\",\"ip_address\":\"214.93.37.238\",\"date\":\"2015-10-05\",\"text\":\"Fusce lacus purus, aliquet at, feugiat non, pretium quis, lectus. Suspendisse potenti. In eleifend quam a odio. In hac habitasse platea dictumst. Maecenas ut massa quis augue luctus tincidunt. Nulla mollis molestie lorem.\"},
{\"id\":\"222\",\"email\":\"karmstrong65@aol.com\",\"ip_address\":\"184.72.254.249\",\"date\":\"2015-10-04\",\"text\":\"Vivamus tortor. Duis mattis egestas metus. Aenean fermentum. Donec ut mauris eget massa tempor convallis. Nulla neque libero, convallis eget, eleifend luctus, ultricies eu, nibh.\"},
{\"id\":\"358\",\"email\":\"rsims9x@jigsy.com\",\"ip_address\":\"85.190.76.246\",\"date\":\"2015-10-04\",\"text\":\"Praesent blandit. Nam nulla.\"}]
}"}

真的不知道为什么json以mockup而不是data开头。

我试图让这个AJAX显示结果:

flag = true;
$(window).scroll(function() {
    if($(window).scrollTop() + $(window).height() == $(document).height()){
        first = $('#first').val();
        limit = $('#limit').val();
        no_data = true;
        if(flag && no_data){
            flag = false;
            $('#loader').show();
            $.ajax({
                url : 'http://dashboard/ajaxtest',
                dataType: "json",
                method: 'post',
                data: {
                   start : first,
                   limit : limit
                },
                success: function(data) {
                    flag = true;
                    $('#loader').hide();
                    if(data.count > 0 ){
                        first = parseInt($('#first').val());
                        limit = parseInt($('#limit').val());
                        $('#first').val( first+limit );
                        $('#timeline-conatiner').append('<h4>'+date+' <small>'+email+'</small></h4>');
                        $.each(data.content, function(key, value ){
                            html = '<div class="content">';
                            html += '<p>'+value.text+'</p>';
                            html += '</div>';
                            $('#timeline-conatiner').append( html );
                        });
                    }else{
                        no_data = false;
                    $('#timeline-conatiner').append('No more data to show');
                    }
                },
                error: function( data ){
                    flag = true;
                    $('#loader').hide();
                    no_data = false;
                    // alert('Something went wrong, Please contact admin');
                    $('#timeline-conatiner').append('Something went wrong, Please contact admin');
                }
            });
        }   
    }
}); 

但问题是它返回No more data to show

我一直在排查

  • 似乎帖子正在发挥作用。显示更改帖子值 在Chrome开发。
  • 我可以使用var_dump
  • 检索(第一个)编码的json

所以我的猜测是AJAX

0 个答案:

没有答案