有没有办法让正确的JSON输出工作? (在ZF1中替代$this-> _heleper-> json-> SendJSON())
public function ajaxSectionAction() {
return new JsonModel(array(
'some_parameter' => 'some value',
'success' => true,
));
}
因为它会引发错误:
> Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException'
> with message 'SmartyModule\View\Renderer\SmartyRenderer::render:
> Unable to render template ...
解决方法:
如果要返回JsonModel,则必须将JsonStrategy添加到view_manager:
//module.config.php
return array(
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
),
)
然后从动作控制器返回一个JsonModel:
public function indexAction()
{
$result = new JsonModel(array(
...
));
return $result;
}
另一种方法,您也可以尝试使用此代码返回每个数据而不进行视图渲染:
$response = $this->getResponse();
$response->setStatusCode(200);
$response->setContent('some data');
return $response;
你可以尝试$response-> setContent(json_encode(array(…)));要么 :
$jsonModel = new \Zend\View\Model\JsonModel(array(...));
$response->setContent($jsonModel->serialize());
标签:php,zend-framework2,zend-framework
来源: https://codeday.me/bug/20190718/1491811.html