Gre*_*rns 4 php zend-framework2
我正在努力学习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;
}
}
Run Code Online (Sandbox Code Playgroud)
但我一直得到这个致命的错误:
( ! ) 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
Run Code Online (Sandbox Code Playgroud)
我一直在寻找这个错误以及在ZF2中进行ajax调用的最佳方法,但是ZF1或ZF2测试结果不断出现并且不起作用.感谢您提供任何建议.
Sam*_*Sam 16
嗯,这个错误几乎意味着它试图访问默认的渲染策略,这很奇怪......你有没有将JsonStrategy添加到你的view_manager?
//module.config.php
return array(
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
),
)
Run Code Online (Sandbox Code Playgroud)
此外,在您的ajax调用中设置正确的接受标头是一个好主意,只接受application/json内容类型.有了这个设置,它应该实际工作.出于好奇,确实modules/__NAMESPACE__/view/__namespace__/documents/get-tree-data.phtml存在吗?