Symfony2 and jquery ajax

I am developing an application using Symfony2 and jQuery as JavaScript FW. I use Twig for templates. I take the template out of the controller, and after selecting with the cursor in the template, I would like the value of the selected tag to be returned to the controller when sending using the submit button in the specified template.

I am using the following jQuery function:

$("MatchedTag").click(function () 
 {
       $(this).toggleClass("highlight");

       var IdOfTag = this.id;  
       $.ajax({
          url: "{{ path('AcmeAcmeBundle_myaction') }}",
          type: "POST",
          data: { "tag_id" : idOfTag },
          success: function(data) {
             //(success) do something...
             //variable "data" contains data returned by the controller. 
          }
       });
});

I think in the controller in myaction I need to use something like $ _POST ["tag_id"] or getrequest (), bindrequest () to get the value, but I really don't know how to do it. Can anyone give me an example. Thank you

+3
source share
1 answer

You can try to get this parameter:

 $request->request->get('tag_id');

Update  simple action

namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;

class HelloController
{
   public function indexAction($name)
   {
     $myParam = $request->request->get('tag_id');
     // write your code here
   }
}
+4
source

All Articles