php模板和json,php – ZF2中没有模板渲染的正确JSON输出

有没有办法让正确的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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值