2012-01-11 99 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