Reputation: 2686
I don't know if this question is duplicated or not. I'm a totally newbie in zend. I want to use an ajax event and when i call the controllername/myfunctionfromthecontroller in url , i want to return a message . I tried this :
$.ajax({
url: "/location/testare",
type: "POST",
data: post_array,
success: function (data, textStatus, jqXHR) {
//data - response from server
},
error: function (jqXHR, textStatus, errorThrown) {
}
});
and in the controller :
public function testareAction()
{
$this->view->String = 'hello world';
}
the firebu return me this : "500 Internal Server Error". Can someone help me with this ?
Upvotes: 1
Views: 211
Reputation: 24
Use helpers!
Example:
If you want to return string using json:
$.ajax({
...
dataType: 'json',
...
});
And in controller:
public function testareAction()
{
$this->_helper->json('hello world');
}
That's all!
More Zend Framework Helper Json
Upvotes: 0
Reputation: 4482
the best way is to use the jsonModel
it looks like
public function testareAction()
{
return new JsonModel(array(
'message' => 'hello world',
));
}
Upvotes: 0
Reputation: 32310
I use this in Zend Framework 1. MaiKaY's solution seems to be fore ZF2:
In the controller, before the output, set the header to expose json output:
$this->getResponse()->setHeader('Content-Type', 'application/json');
Disable all the layout stuff:
$this->_helper->layout->disableLayout();
And if you want to use echo \Zend_Json::encode($yourOutput);
instead of using a view for the json, use this:
$this->_helper->viewRenderer->setNoRender(true);
Upvotes: 4