我努力學習ZF2。我有一個使用Ajax獲取一些數據的頁面。 ZF2函數應該返回一個JSON字符串。ZF2:返回JSON只針對AJAX調用
<?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: 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貝塔滾滾而來不起作用。感謝您提供任何建議。
感謝我無法相信我錯過了羅布·艾倫的DevNotes的一部分。是的,我有get-tree-data.phtml,但使用JsonModel它沒有找到它。 –
冒先風險,我有一些代碼來幫助與zf2的AJAX請求:http://github.com/superdweebie/jsonController。它有三個新的抽象控制器來處理jsonRest,jsonRPC和jsonp請求。 – superdweebie