2012-01-11 82 views
2

我不知道为什么这双JSON响应都没有成功:双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响应的解决方案?有一个没有问题

回答

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; 
} 
在profile.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(); 
} 
+2

或在对象 – 2012-01-11 19:18:20

+0

确切地说应该是[{ “first_content”: “内容”,...}包住阵列,{”第二个内容“:”内容“,...}] – 2012-01-11 19:18:23

+0

为什么你想要两个请求,当你可以完成一个.. – Baz1nga 2012-01-11 19:27:37