I am trying to create a simple ZF 1 chatbox application, but i'm getting an internal server 500 error when trying to insert chat posts into a db. Also the data from an ajax is being send as text/html even though i it defined to be json. As the firebug's network tab shows, i get in a response my whole html code. Could somebody please help me. Here is the controller's code:
class AjaxController extends Zend_Controller_Action
{
public function init(){
$contextSwitch = $this->_helper->getHelper('AjaxContext');
$contextSwitch->addActionContext('index','json')
->initContext();
}
public function indexAction()
{
if($this->getRequest()->isXmlHttpRequest()) {
$data = $this->getRequest()->getPost();
$chatModel = new Application_Model_DbTable_Chat();
$chatModel->createPost($data);
$chatPosts = $chatModel->fetchAll();
return $this->_helper->json->sendJson($chatPosts);
}
}
}
here is the ajax:
$(document).ready(function(){
$('#chat').submit(function(){
var message = $('#message').val();
$.ajax({
type: 'POST',
dataType: 'json',
url: '/ajax/index',
data: { 'message': message },
success: function(data) {
$('#outputArea').html(data);
},
failure: function(){ alert("does not work");
}
});
return false;
});
});
the model:
class Application_Model_DbTable_Chat extends Zend_Db_Table_Abstract
{
protected $_name = 'chat';
public function createPost($data){
$insertData = array(
'messages' => $data['message']
);
return $this->insert($insertData);
}
}
and a form(which I have written manually in a layout file):
<form id="chat" action="/ajax/index" method="post">
<h2>Chat</h2>
<p id="outputArea"></p>
<textarea name="message" id="message" cols="31" rows="3"></textarea>
<input type="submit" value="post a message" name="submit" />
</form>