Symfony2 Ajax and jQuery

I am developing an application using Symfony2 and twig for templates. I also use ajax. This is part of my code in my controller:

    public function testuaanotatuAction(Request $request)
    { 
      if ($request->isXmlHttpRequest())
      {
       return $this->forward('AnotatzaileaAnotatzaileaBundle:Page:Interpretatu');
      }

    // Some code.....
            return $this->render('AnotatzaileaAnotatzaileaBundle:Page:AnotatuInterpretazio.html.twig',
                                   array('Azpimarratu' => $Markagarria->getMarkIdent()));

}


    public function InterpretatuAction()
    {
      return $this->render('AnotatzaileaAnotatzaileaBundle:Page:FAQ.html.twig');
    }

And here is my code in AnotatuInterpretazio.html.twig: "I am making an Ajax call using jQuery:

<script type='text/javascript'>
          $("#ButtonId").click(function () 
                             {
                               $.ajax({
                                        url: "{{ path('AnotatzaileaAnotatzaileaBundle_testuaanotatu') }}",
                                        type: "POST"
                                       });      
                         });
    </script>

As you can see, I'm going to do here to call InterpretatuAction through the AnotatuInterpretazio.html.twig template. Then InterpretatuAction will call another template. It doesn't work, any idea?

+3
source share
3 answers

You need to use the function .successin your code. If you do not, nothing will happen.

+2
source

, , JavaScript path(), .

+1

You can use the method path(), always if you are in a branch template (although you are in a script) inside {{ }}. It would be like this:

<a href="{{ path('usuario_logout') }}">Cerrar sesión</a>

This code is part of one of my projects and works.

0
source

All Articles