Zend Framework 2 AJAX: return JSON response from controller action. The proper way.
There is a lot of solutions over the web on how to return data on AJAX request in your Zend Framework 2 application. But none of them seems to be aware of the "right way" described in Zend Framework 2 manual - http://framework.zend.com/manual/2.0/en/modules/zend.mvc.examples.html#returning-early
Implementation example:
Implementation example:
<?php namespace <YOUR_MODULE>\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\Json\Json; class AjaxController extends AbstractActionController { public function testAction() { $data = array( 'result' => true, 'data' => array() ) return $this->getResponse()->setContent(Json::encode($data)); } }
Header ouput is html:
ReplyDeleteAccept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Hey Nice Article.
ReplyDeleteThis is the kinda article I like. Short as hell, but gets the job done :) call me lazy, don't care, it was good ;)
ReplyDeleteThnxx a lott!!!! :D
ReplyDeleteHappy to share your blog! Good article! I love it
ReplyDeleteThis way the header is not automatically set, is there a better way?
ReplyDeletechange the content type like this:
ReplyDelete$this->getResponse()->getHeaders()->addHeaders(array('Content-Type'=>'application/json;charset=UTF-8'));
return $this->getResponse()->setContent(Json::encode($ata));
Its good work , is it the perfect way ..?
ReplyDelete