If your AJAX is returning JSON you can use JSON action helper:
$this->_helper->json($data);
This helper will json_encode
your $data, output it with JSON headers and die at last, so we getting clean JSON returned from action without layout and view rendering.
f.e. I am using this construction in action beginning to avoid multiple ACL checks for different actions just-for-ajax
public function photosAction() {
if ($this->getRequest()->getQuery('ajax') == 1 || $this->getRequest()->isXmlHttpRequest()) {
$params = $this->getRequest()->getParams();
$result = false;
switch ($params['act']) {
case 'deleteImage':
//deleting something
...
$result = true; //ok
break;
default :
$result = array('error' => 'Invalid action: ' . $params['act']);
break;
}
$this->_helper->json($result);
}
// regular action code here
...
}