3

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?

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

And what do I have to do,what can I do, inside the success function? I s it really necesary?
You should take a look at the documentation, everything is explained api.jquery.com/jQuery.ajax
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.