一个Symfony2新手问题。
我希望将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.html.twig模板+ jsonAction控制器输出.json文件
/**
* 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) {...}
总是失败。 欢迎帮助。
答案 0 :(得分:0)
我觉得这种方法是这样的:
function ajaxCall()
{
$.ajax({
type: "GET",
url: // URL for the PHP controller,
dataType: 'json',
success: function(data){
// give the data (a JSON in your case) to the Simile's js function
}
});
}
注意:这是使用jQuery的AJAX。您可以使用任何其他JavaScript库甚至原始javascript来执行AJAX。
祝你好运