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条)

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来