如何在twig view symfony 3上的jquery prepend事件之后存储数据?

如何在twig view symfony 3上的jquery prepend事件之后存储数据?,jquery,twig,symfony,Jquery,Twig,Symfony,当我单击“发布”按钮时,我遇到了一个问题,我希望数据出现在我的页面中,而不刷新页面,并添加到我希望将这些数据存储在数据库中的数据中,下次访问此页面时,我会找到这些数据 <form method="post"> <textarea id="txt" class="form-control input-lg no-border" rows="2" placeholder="W

当我单击“发布”按钮时,我遇到了一个问题,我希望数据出现在我的页面中,而不刷新页面,并添加到我希望将这些数据存储在数据库中的数据中,下次访问此页面时,我会找到这些数据

    <form method="post">
                    <textarea id="txt" class="form-control input-lg no-border" rows="2"
                              placeholder="What are you doing?..."></textarea>
        <script>
            $(document).ready(function() {
                $("#btnpost").click(function () {
                    var text = $('#txt').val();
                    $.ajax({
                        type: 'POST',
                        url: '{{ path('group_addpub') }}',
                        data: {desc: text}
                    });
                    $("#publication").prepend('<div class="panel panel-success rounded shadow" style="text-align: left;margin-bottom: 5px;">' +
                            '<div class="panel-heading no-border">'+
                            '<div class="pull-left half">'+
                            '<div class="media" style="text-align: left;">'+
                            '<div class="media-object pull-left" style="margin-top: 35px;">'+
                            '<img src="http://bootdey.com/img/Content/avatar/avatar2.png" style="width: 40px;height: 40px;">' +
                            '</div>'+
                            '</div>'+
                            '</div>'+
                            '<a href="">test profile</a>'+
                            '<span class="text-white h6" style="display: block; color: black;">on 8th June, 2014</span>'+
                            '<br>'+
                            '<span style="color: black;margin-bottom: 10px;word-break: break-all  ">'+text+ '</span>'+


                            '</div>'+
                            '</div>'+
                            '<div class="panel-footer">'+

                            '<form action="#" class="form-horizontal">'+
                            '<div class="form-group has-feedback no-margin">'+
                            '<div style="text-align: right;margin-top: 32px;">'+
                            '<a href=""><img src="{{ asset('Groupe/img/like-icon.png') }}" style="width:5%;"></a>'+
                            '<a href="" ><img src="{{ asset('Groupe/img/Unlike-icon.png') }}" style="width:5%;"></a>'+
                            '</div>'+
                            '<input class="form-control" type="text" placeholder="Votre commentaire ici..." style="width: 95%;margin-left: 10px;">'+
                            '</div>'+
                            '</form>'+
                            '</div>');


                    $('#txt').val('');

                });
            });
        </script>

    </form>
这是我的路由文件

  group_group_photos:
path:     /photos
defaults: { _controller: GroupGroupBundle:Group:photosGroupe}

  group_new:
path:     /{id}
defaults: { _controller: GroupGroupBundle:Group:newGroupe}

 group_addpub:
path:     /addpub
defaults: { _controller: GroupGroupBundle:Group:addpubication}

你有了碎片,现在把它们正确地连接起来

$.ajax({
    type: 'POST',
    url: "{{ path('group_addpub') }}", // NOTE the " instead of '.
    data: {desc: text} // You'll access the text variable by it's property name, 'desc'.
});
在控制器中:
公共函数addPublicationAction(请求$Request)
{
$em=$this->getDoctrine()->getManager();
$des=$request->get('desc');//setDescription($des);
$em->persist($publication);
$em->flush();
}
旁注:混合单引号和双引号:
'{path('group_addpub')}}}
这里的问题是,当细枝解析这个时,它会看到
'string'变量'string'
,这没有任何意义。

有人可以帮我吗我该怎么办url@Was'siimbenhsen尝试设置AJAX
url:'/addpub'
,然后在控制器中添加
转储($request->request);die;
查看您的控制器正在接收的内容。似乎没有显示任何内容。我的控制器似乎没有收到data@Was'Siimbenhsen,此控制器操作的完整路径是什么?GroupBundle的前缀是否为
/group/addpub
?请尝试将其更改为
url:'/groupe/addpub',
$.ajax({
    type: 'POST',
    url: "{{ path('group_addpub') }}", // NOTE the " instead of '.
    data: {desc: text} // You'll access the text variable by it's property name, 'desc'.
});
public function addpubicationAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $des = $request->get('desc'); // <-- Here is the text variable.

    $publication = new Publication();
    $publication->setDescription($des);
    $em->persist($publication);
    $em->flush();
}