【问题标题】:Zend response application/json utf-8Zend 响应应用程序/json utf-8
【发布时间】:2011-06-22 20:04:55
【问题描述】:

通常在 xhr 操作中我使用此代码

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

【问题讨论】:

    标签: php json zend-framework


    【解决方案1】:

    这样做是个好习惯。如果不这样做,您可能看不到任何问题。这取决于您发送的数据类型。

    有一个更短的方法来做你正在做的事情(禁用布局并设置正确的标题):

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

    【讨论】:

    • 在视图/助手中我只看到 $response = Zend_Controller_Front::getInstance()->getResponse(); $response->setHeader('Content-Type', 'application/json', true);返回$数据;所以.....
    • 似乎sendJson 不允许您设置特定的字符集(例如utf-8)。
    • 看着 Zend_Controller_Action_Helper_Json 我看不到任何关于标头 'application/json;charset=UTF-9' 的信息。
    【解决方案2】:

    您最好使用contextswitch/ajaxcontext helper。请参阅第一个答案中的示例there

    【讨论】:

      猜你喜欢
      • 2018-11-15
      • 1970-01-01
      • 2021-07-30
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 2015-10-23
      相关资源
      最近更新 更多