Symfony2新手問題。Symfony2 + JS時間軸。如何將json文件傳遞給視圖?
我希望將Simile的時間線http://www.simile-widgets.org/timeline/用於我的Symfony2 web應用程序。 要做到這一點,我需要輸出以.json文件,並把它我timeline.html.twig視圖中傳遞給JS代碼:
...
tl.loadJSON("http://localhost:{{ <my json file> }}?"+ (new Date().getTime()), function(json, url) {...}
我能夠輸出使用JSON一個以.json文件。 html.twig模板+一jsonAction控制器
/**
* Lists all Post entities.
*
* @Route("/json", name="Mario_Post_json")
* @Method("GET")
* @Template()
*/
public function jsonAction()
{
$response = new Response();
$em = $this->getDoctrine()->getEntityManager();
$query = $this->getDoctrine()
->getRepository('MarioBlogBundle:Post')
->createQueryBuilder('e')
->getQuery();
$results = $query->getArrayResult();
$response->setContent(json_encode($results));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
我能夠通過加載和渲染一個靜態文件上傳.json:
tl.loadJSON("http://localhost{{ asset('bundles/marioblog/js/timeline/timeline.json') }}?"+ (new Date().getTime()), function(json, url) {...}
但如果我嘗試的類似
變種tl.loadJSON("http://localhost/symfony/web/app_dev.php/Mario/Post/jsonout?"+ (new Date().getTime()), function(json, url) {...}
它總是失敗。 歡迎光臨幫助。
我認爲JSON是在你模板渲染後(小枝作業後)提取的。因此,您需要在JavaScript級別執行JSON抓取(例如,通過發送執行PHP控制器的AJAX請求).PS:它是一個新手註釋。 –
我的理解是,tl.loadJSON是在JS級別,不是嗎? – mario
你能告訴我們:你的JS代碼如何執行你的PHP控制器?我不幸仍然無法在你的代碼中看到它。 –