简体   繁体   中英

Zend response application/json utf-8

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);

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM