Haritz
Haritz

Reputation: 1752

Symfony2 Ajax and Jquery

I am developing an application using Symfony2 and twig for the templates. I am also using 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 then this is my code in AnotatuInterpretazio.html.twig' were I do the Ajax call using JQuery:

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

As you can see what I intend to do here is to call InterpretatuAction through the template AnotatuInterpretazio.html.twig. Then InterpretatuAction will call another template. It doesnt work, any idea?

Upvotes: 3

Views: 2762

Answers (3)

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

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

This code is part of one of my projects and it's working.

Upvotes: 0

Pedro
Pedro

Reputation: 11

I think the problem is that JavaScript can't resolve the method path(), this method is useful only in the server side for twig.

Upvotes: 1

romainberger
romainberger

Reputation: 4558

You need to use a .success function in your code. If you don't, nothing is going to happen

Upvotes: 2

Related Questions