2011-09-03 15 views
7

में एन्कोडिंग प्रतिक्रिया भेजना मैं एक परियोजना में बहुत सारे jQuery का उपयोग कर रहा हूं।जेसन

मेरे पास एक जावास्क्रिप्ट फ़ंक्शन है जो एक नियंत्रक को AJAX अनुरोध करता है जो JSON में डेटा देता है।

मैं उपयोगकर्ता को सूचित करने वाला एक उपयोगकर्ता के अनुकूल संदेश प्रदर्शित करना चाहता हूं कि उसके पास अभी तक कोई जानकारी संग्रहीत नहीं है। लेकिन मैं उलझन में हूं कि जेएसओएन में प्रतिक्रिया कैसे भेजनी है, इसलिए मेरा जावास्क्रिप्ट फ़ंक्शन यह निर्धारित कर सकता है कि उपयोगकर्ता को जानकारी प्रदर्शित करने के लिए है या नहीं।

यहाँ मेरी जावास्क्रिप्ट समारोह है:

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 उत्पादन उत्पन्न करने के लिए किया जाता है, लेकिन वाक्य रचना तो टूट मैं नहीं पढ़ा है यह जावास्क्रिप्ट, के साथ, लेकिन एक बार जब मैं उपरोक्त विधि से निम्न कोड से छुटकारा पाता हूं तो यह सामान्य रूप से

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

मैं HTTP हेडर के बजाय वेबपेज में केवल json डेटा भेज सकते हैं? – Rajan

उत्तर

14

यदि आप अधिक सामान आउटपुट करना चाहते हैं, तो आपको केवल प्रतिक्रिया में एक बार json_encode ($ डेटा) का उपयोग करना होगा, आपको अपने डेटा को एक सरणी में विलय करने और उसे भेजने की आवश्यकता है।

संपादित करें:

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

तब जे एस में आप कुंजी "pheeds" और "रेस" के साथ एक सरणी मिल जाएगा: स्पष्ट एक तरह से आप इसे इस प्रकार है कर सकता हो,।

हालांकि यह थोड़ा व्यावहारिक महत्व का हो सकता है, मैं भी इससे पहले कि आप json इनकोडिंग स्ट्रिंग गूंज ऐसा करने की सिफारिश करेंगे:

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