gpt4 book ai didi

php - ZF2 : Return JSON only for Ajax Call

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:55:08 32 4
gpt4 key购买 nike

我正在努力学习 ZF2。我有一个使用 Ajax 获取一些数据的页面。 ZF2 函数应返回一个 JSON 字符串。

<?php
namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;

class DocumentsController extends AbstractActionController {

public function indexAction() {

}

public function getTreeDataAction() {
$json = new JsonModel(array(
'title' => 'Some Title'
));
return $json;
}

}

但我一直收到这个 fatal error :

( ! ) Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "application/documents/get-tree-data"; resolver could not resolve to a file' in ../vendor/ZF2/library/Zend/View/Renderer/PhpRenderer.php on line 451

我一直在寻找这个错误以及在 ZF2 中进行 ajax 调用的最佳方法,但是 ZF1 或 ZF2 beta 的结果不断出现并且不起作用。感谢您提供任何建议。

最佳答案

嗯,这个错误几乎意味着它试图访问默认渲染策略,这很奇怪......你是否将 JsonStrategy 添加到你的 view_manager?

//module.config.php
return array(
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
),
)

此外,为您的 ajax 调用设置正确的接受 header 以仅接受 application/json 内容类型是个好主意。有了这一套,它应该可以正常工作。出于好奇,modules/__NAMESPACE__/view/__namespace__/documents/get-tree-data.phtml 是否存在?

关于php - ZF2 : Return JSON only for Ajax Call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12612414/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com