Zend response application/json utf-8

自古美人都是妖i 提交于 2019-12-09 18:29:32

问题


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

回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/6445843/zend-response-application-json-utf-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!