5

usually in a xhr action I use this code

$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$response = $this->getResponse();
$response->setHeader('Content-type', 'application/json', true);
return $response->setBody(Zend_Json::encode($data));

I'm wondering if it need utf-8 encoding like this

$response->setHeader('Content-type', 'application/json;charset=UTF-8', true);
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
Whisher
  • 31,320
  • 32
  • 120
  • 201

2 Answers2

11

It would be good practice to do so. You may not see any problems if you don't. It depends what kind of data you are sending.

there is a much shorter way to do what you are doing (Disables layouts and sets the right headers):

 $this->_helper->json->sendJson($data);
datasage
  • 19,153
  • 2
  • 48
  • 54
  • in view/helper I only see $response = Zend_Controller_Front::getInstance()->getResponse(); $response->setHeader('Content-Type', 'application/json', true); return $data; so ..... – Whisher Jun 22 '11 at 20:25
  • It seems like `sendJson` does not set allow you to set a specific charset (e.g. utf-8). – Till Jul 02 '12 at 12:54
  • Looking at `Zend_Controller_Action_Helper_Json` I can't see anything about headers 'application/json;charset=UTF-9'. – Kirzilla Sep 12 '12 at 14:39
3

You should better use contextswitch/ajaxcontext helper. See example in first answer there.

Community
  • 1
  • 1
Dmytro Zavalkin
  • 5,265
  • 1
  • 30
  • 34