我正在通过 Jquery 在 Zend Framework 中创建一个非常简单的聊天框,并将内容加载到$('#outputArea')
本质上是一个 div 的 div 中$.load()
,但我得到一个 json 对象,我只需要提取一个消息值。我得到了什么$.load()
这是:
[
{
"id": "1",
"messages": "message 1"
},
{
"id": "2",
"messages": "message 2"
},
{
"id": "59",
"messages": "d"
},
{
"id": "60",
"messages": "w"
},
{
"id": "61",
"messages": "fff"
},
{
"id": "62",
"messages": "d"
},
{
"id": "63",
"messages": "df"
},
{
"id": "64",
"messages": "dfs"
},
{
"id": "65",
"messages": "dw"
},
{
"id": "66",
"messages": "dw11"
},
{
"id": "67",
"messages": "dw112"
},
{
"id": "68",
"messages": "111"
},
{
"id": "69",
"messages": "111"
},
{
"id": "70",
"messages": "dd"
},
{
"id": "71",
"messages": "sddd"
},
{
"id": "72",
"messages": "\n\n"
},
{
"id": "73",
"messages": "ss"
}
]
我怎样才能做到这一点?谢谢你。这是jquery代码:
$(document).ready(function(){
$('#outputArea').load('/index/index');
$('#chat').submit(function(){
var message = $('#message').val();
if(message === ''){
$('#error-message').css('display','inline');
return false;
}
$.post('/index/index',{'message':message},function(data){
$('#error-message').css('display','none');
var n = data.length;
$('#outputArea').append('<div>' + data[n-1].messages + '</div>' + '<br />');
console.log(data);
},'json');
return false;
});
});
以及负责解析JSON的php索引控制器部分:
if($this->_request->isXmlHttpRequest()){
$this->_helper->viewRenderer->setNoRender();
$this->_helper->getHelper('layout')->disableLayout();
$model = new Application_Model_DbTable_Chat();
$data = $this->_request->getParam('message');
//$data = json_decode($data);
$model->createPost($data);
$json = $model->fetchAll();
header('Content-type:application/json');
//print_r($json);
foreach ($json as $key => $value) {
}
echo $this->_helper->json->sendJson($json);
}