duanliang4009 2014-01-23 06:37
浏览 56
已采纳

如何使用Javascript中的参数值生成Symfony 2 twig URL?

I want to append query string parameters from javascript (jquery) to symfony 2 url. I want to pass the value of selected radio button to ajax request e.g.in plain php I would have done like

$(document).ready(function() {
    $('.id_radio').click(function() {                      
        $.ajax({
            'url': 'example.php?id='+($this).val(),
            'success': function(r) {
                $('#div1').html(r);
            }
        });
    });

How to generate such URL in symfony2 twig ? We generate URL in symfony 2 twig like

{{ path('example_path', {'id': id}) }} where id is twig variable.

I tried using FOSJsRoutingBundle but don't know if it is for same problem and not sure how to use it ?

I tried below but it is not working.

$('.id_radio').click(function() {
    alert(Routing.generate('example_path', {
        'id': $(this).val()
        }));
    $.ajax({
        'url': Routing.generate('example_path', {
            'id': $(this).val()
            }),
        'success': function(r) {
            $('#div1').html(r);
        }
    });
});

Solution mentioned below by koskoz worked, I did not pass options={"expose"=true} but after adding it worked.

But there is one more problem. It works only until we do not refresh page. If we refresh page, it does not work. To make it work, I have to delete symfony cache files.

It does not work in other browser if we have already opened it in one browser.

  • 写回答

3条回答 默认 最新

  • dream890110 2014-01-23 08:11
    关注

    Instead of

    /**
     * @Route("/example/{name}", name="example_path", options={"expose"=true})
     * @Template
     */
    public function exampleAction($name)
    {
    
    }
    

    and using FOSJsRoutingBundle

    $('.id_radio').click(function() {
        alert(Routing.generate('example_path', {'id': $(this).val()}));
        $.ajax({
            'url': Routing.generate('example_path', {'id': $(this).val()}),
            'success': function(r) {
                $('#div1').html(r);
            }
        });
    });
    

    Below worked better for me,

         /**
         * @Route("/example", name="example_path", options={"expose"=true})
         * @Method({"GET"})
         * @Template 
         */
        public function exampleAction()
        {
    
    
        }
    
    $('.id_radio').click(function() {
        $.ajax({
            'url': '{{ path('example_path') }}',
            'data': 'name='+$(this).val(),
            'type': 'GET',
            'success': function(r) {
                $('#div1').html(r);
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决