2012-03-30 80 views
0

我想弄清楚爲什麼當它獲取到我的控制器中的getLast5Messages函數時,它會返回以下內容,但在javascript中的響應中會放入頁面的html。我不明白。不顯示數據數組

[{"id":"1","subject":"Testing","firstName":"Kevin","lastName":"Smith","dateSent":"March 30th, 2012","messageRead":"0"}] 

JS:

function getMessages() 
{ 
$('.mail').empty(); 
$.get('dashboard/getLast5Messages', function(data) 
{ 
    if(data.length > 0) 
    { 
     $.each(data, function(x) 
     { 
      if (data[x]['messageRead'] == 0) 
      { 
       $('.mail').append('<li><a href="pmsystem/viewmessage/'+data[x]['id']+'"><strong>Received: '+data[x]['dateSent']+'</strong><small>'+data[x]['subject']+'</small><small>From: '+data[x]['firstName']+' '+data[x]['lastName']+'</small></a></li>'); 
      } 
      else 
      { 
       $('.mail').append('<li class="read"><a href="pmsystem/viewmessage/'+data[x]['id']+'"><strong>Received: '+data[x]['dateSent']+'</strong><small>'+data[x]['subject']+'</small><small>From: '+data[x]['firstName']+' '+data[x]['lastName']+'</small></a></li>');  
      } 
     }); 
    } 
    else 
    { 
     $('.mail').append('<li>No Messages</li>'); 
    } 
}, 'json'); 
} 

PHP:

public function getLast5Messages() 
{ 
    echo $this->pmmodel->getLast5Messages($this->session->userdata('userID')); 
} 

/** 
* Retrieve last 5 messages in inbox 
* 
* @param integer 
* @return object 
*/ 
function getLast5Messages($userID) 
{ 
    // Check args 
    if (!is_numeric($userID)) 
    { 
     throw new Exception('Non-numeric $userID provided to getLast5Messages()'); 
    } 

    $this->db->select('pm.id'); 
    $this->db->select('pm.subject'); 
    $this->db->select('users.firstName'); 
    $this->db->select('users.lastName'); 
    $this->db->select("DATE_FORMAT(pm.dateSent, '%M %D, %Y') AS dateSent", false); 
    $this->db->select('pmr.messageRead'); 
    $this->db->from('usersPersonalMessages AS pm'); 
    $this->db->join('users', 'users.userID = pm.senderID'); 
    $this->db->join('usersPersonalMessagesRecipients AS pmr', 
     'pm.id = pmr.usersPersonalMessagesID'); 
    $this->db->where('pmr.userID', $userID); 
    $this->db->order_by('pm.dateSent', 'desc'); 
    $this->db->limit(5); 
    $query = $this->db->get(); 
    if ($query->num_rows() > 0) 
    { 
     return $query->result(); 
    } 
    return array(); 
} 

我還是沒能想出解決辦法?有沒有人有任何想法可能造成這種情況?

+0

你是什麼意思「在它把頁面的HTML的JavaScript反應」?你能更清楚地表達你的問題嗎? – 2012-03-30 20:21:59

+0

查看開發人員控制檯中的XHR請求,以查看從服務器返回的究竟是什麼。看起來像你的'getLast5Messages()'PHP函數可能被封裝在一個HTML文檔中。 – Jasper 2012-03-30 20:22:04

+0

它不是。 http://www.kansasoutlawwrestling.com/kowmanager/dashboard/getLast5Messages – 2012-03-30 20:24:44

回答

0

這是

public function getLast5Messages() 
{ 
    echo json_encode($this->pmmodel->getLast5Messages($this->session->userdata('userID'))); 
}