2011-09-03 95 views
7

我正在使用大量的jQuery。在json中發送編碼響應

我有一個JavaScript函數,它向JSON中返回數據的控制器發出ajax請求。

我想顯示一條用戶友好的消息,通知用戶他/她還沒有存儲信息。但我很困惑如何在JSON中發送響應,以便我的javascript函數可以確定用戶是否具有要顯示的信息。

這是我的javascript函數:

function latest_pheeds() { 
    var action = url+"pheeds/latest_pheeds"; 
    $('#pheed-stream').html('<div class="loading"></div>'); 
    $('.loading').append('<img src="'+pheed_loader_src+'" />'); 
    $.ajax({ 
     url:action, 
     type:'GET', 
     dataType:'json', 
     error: function() { 
     }, 
     success:function(data) { 
      $('.loading').fadeOut('slow'); 
      $.each(data,function(index,item) { 
      $('#pheed-stream').append 
      (
      '<div class="pheed" id="'+item.pheed_id+'">'+ 
      '<p><a class="user_trigger" href="users/info/'+item.user_id+'">' 
      +item.user_id+'</a></p>'+ 
      '<p>'+item.pheed+'</p>'+ 
      '<div class="pheed_meta">'+ 
      '<span>'+item.datetime+' Ago</span>'+ 
      '<span class="cm">'+item.comments+ 
      '<img class="comment_trigger" src="/pheedbak/assets/img/comment.png" title="Click to comment on pheed" onclick="retrieve_comments('+item.pheed_id+')">'+ 
      '</span>'+ 
      '<span>'+item.repheeds+ 
      ' Repheeds'+ 
      '<img class="repheed_trigger" src="/pheedbak/assets/img/communication.png" title="Click to repheed" onclick="repheed('+item.pheed_id+')">'+ 
      '</span>'+ 
      '<span>'+ 
      'Favourite'+ 
      '<img class="favourite_trigger" src="/pheedbak/assets/img/star.png" title="Click to make this a favourite" onclick="favourite_pheed('+item.pheed_id+')" />'+ 
      '</span>'+ 
      '</div>'+ 
      '</div>' 
      ); 
     }); 
     } 
    }); 
} 

而繼承人的控制器功能的AJAX請求到

function latest_pheeds() { 
    //Confirm if a user is logged before allowing access 
    if($this->isLogged() == true) { 
     //load the pheed model for database interaction 
     $this->load->model('pheed_model'); 
     //load user model 
     $this->load->model('user_model'); 
     //load comment model 
     $this->load->model('comment_model'); 
     //store the pheeds to a the $data variable 
     $data = $this->pheed_model->get_latest_pheeds(); 
     //Load the date helper to calculate time difference between post time and current time 
     $this->load->helper('date'); 
     //Current time(unix timetamp) 
     $time = time(); 
     //pheeds 
     $pheeds = array(); 
     if(count($data) > 0) { 
      foreach($data as $pheed) { 
       $row['pheed_id'] = $pheed->pheed_id; 
       $row['user_id'] = $this->user_model->return_username($pheed->user_id); 
       $row['pheed'] = $pheed->pheed; 
       $row['datetime'] = timespan($pheed->datetime,$time); 
       $row['comments'] = $this->comment_model->count_comments($pheed->pheed_id); 
       $row['repheeds'] = $pheed->repheeds; 
       $pheeds[] = $row; 
      } 

      echo json_encode($pheeds); 
      $response['response'] = "Ok"; 
      $res[] = $response; 
      echo json_encode($res)."\n"; 
     } 
    } else { 

    } 

它生成JSON輸出製成,但語法被打破,所以我不能讀它與JavaScript, 但一旦我擺脫了以上代碼從上面的方法它通常工作

$response['response'] = "Ok"; 
    $res[] = $response; 
    echo json_encode($res)."\n"; 
+0

我可以在HTTP標頭的網頁只發送JSON數據? – Rajan

回答

14

你只能在響應中使用json_encode($ data),如果你想輸出更多的東西,你需要將你的數據合併到一個數組併發送它。

編輯:要清楚,一個方式,你可以做到這一點是這樣的:

echo json_encode(array('pheeds' => $pheeds, 'res' => $res)); 

然後在JS,你會得到的鑰匙「pheeds」和「資源」的數組。

雖然它可能是沒有什麼實際意義,我也建議您呼應JSON編碼字符串之前這樣做:

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