双json回应

时间:2012-01-11 19:11:23

标签: javascript jquery ajax

我不知道为什么这个双json响应不成功:

[{"first_content":"content",...}][{"second_content":"content",...}]     

所以我收到了消息Oops! Try Again

if(isset($_GET['start'])) {
    echo get_posts($db, $_GET['start'], $_GET['desiredPosts']);
    echo get_posts1($db, $_GET['start'], $_GET['desiredPosts']);
    $_SESSION['posts_start']+= $_GET['desiredPosts'];
    die();
}


var start = <?php echo $_SESSION['posts_start']; ?>;
var desiredPosts = <?php echo $number_of_posts; ?>;
var loadMore = $('#load-more');

loadMore.click(function () {
                loadMore.addClass('activate').text('Loading...');
                $.ajax({
                    url: 'profile.php',
                    data: {
                        'start': start,
                        'desiredPosts': desiredPosts
                    },
                    type: 'get',
                    dataType: 'json',
                    cache: false,
                    success: function (responseJSON, responseJSON1) {
                        alert(responseJSON);
                        loadMore.text('Load More');
                        start += desiredPosts;
                        postHandler(responseJSON, responseJSON1);
                    },
                    error: function () {
                        loadMore.text('Oops! Try Again.');
                    },
                    complete: function () {
                        loadMore.removeClass('activate');
                    }
                });
            });

获得双重json响应的解决方案是什么?有一个没有问题

1 个答案:

答案 0 :(得分:6)

你调用它们的“双JSON响应”基本上是无效的JSON。你应该有类似的东西:

{"first_content":"content", "second_content":"content",...}

或者有几个人提到:

[{"first_content":"content",...}, {"second_content":"content",...}]

您可能需要修改一些服务器端代码,您的get_posts函数可能会返回PHP数组而不是JSON数组。例如:

function get_posts(){
    $array = array('content' => 'foo', 'title' => 'bar');
    return $array;
}

然后在你的个人资料中.php:

if(isset($_GET['start'])) {
    $posts = get_posts($db, $_GET['start'], $_GET['desiredPosts']);
    $posts1 = get_posts1($db, $_GET['start'], $_GET['desiredPosts']);
    echo array($posts, $posts1);
    $_SESSION['posts_start']+= $_GET['desiredPosts'];
    die();
}