在jquery中的twig无法读取jquery变量 [英] Twig in jquery not able to read jquery variable

查看:102
本文介绍了在jquery中的twig无法读取jquery变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery帖子,但是我使用twig语法的路径部分有问题。以下是代码:

I am trying to use jQuery post but having issue with the path part where I am using a twig syntax. Here is the code:

messageSender.click(function(e) {
            e.preventDefault();
            var threadId     = $(this).find('a').data('id');

            $.post("{{ path('messages_view', {id: "+threadId+"}) }}", function(data, status) {
                console.log(data);
            });
        });

这不会得到 threadId ,而是将文字+'threadId'+存储到路径中的id变量。

This doesn't get the value of the threadId, instead it stores the literal ""+'threadId'+"" to the id variable in path.

推荐答案

你的函数在客户端(js)

,你的路径在服务器端(twig)

Your function is in client side (js)
and your path is in server side (twig)

所以这不起作用。

要解决此问题,您可以使用 FOSJsRoutingBundle doc )。

To solve this, you can use FOSJsRoutingBundle (doc).

使用你的代码,你可以得到类似的东西:

With your code, you can have something like that:

messageSender.click(function(e) {
    e.preventDefault();
    var threadId     = $(this).find('a').data('id');
    var url = Routing.generate('messages_view', { id: threadId});

    $.post(url, function(data, status) {
        console.log(data);
    });
});

这篇关于在jquery中的twig无法读取jquery变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆