我正在试图找出为什么当它到达我的控制器中的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 :(得分:0)
是
public function getLast5Messages()
{
echo json_encode($this->pmmodel->getLast5Messages($this->session->userdata('userID')));
}