2011-06-22 68 views
5
通常

在我使用此代碼的Zend響應應用程序/ JSON UTF-8

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

如果需要UTF-8編碼這樣

$response->setHeader('Content-type', 'application/json;charset=UTF-8', true); 

回答

12

我想知道一個XHR行動這將是好的做法是這樣做的。如果你不這樣做,你可能看不到任何問題。這取決於您發送的數據類型。

有一個更短的方式做你正在做什麼(禁用佈局,並設置合適的報頭):

$this->_helper->json->sendJson($data); 
+0

in view/helper我只看到$ response = Zend_Controller_Front :: getInstance() - > getResponse(); $ response-> setHeader('Content-Type','application/json',true); 返回$ data;所以..... – Whisher

+0

btw感謝回覆:) – Whisher

+0

好像'sendJson'沒有設置允許你設置一個特定的字符集(例如utf-8)。 – Till