Symfony 3.4中的Ajax请求

i am using symfony 3.4 , i want to execute a controller action on a button click in my twig template , that's why i used ajax for that : first i added the ajax route :

ajax_route:
    path:     /ajax_request
    defaults: { _controller: offerBundle:Default:ajax }
    methods: [post]

then the controller action :

    public function ajaxAction(Request $request)
    {
        $personnage = $request->request->get('personnage');
        dump($request->request);
        $wishlist = new wishlist();
        $wishlist->setUserid($personnage);
        $wishlist->setOfferid(1);
        $em=$this->getDoctrine()->getManager();
        $em->persist($wishlist);
        $em->flush();
    }

the ajax part :

    <script>
        $('.btn').click( function(){
            var personnage = 3;
            $.ajax({
         url: "{{ path('ajax_route') }}",
         type: "POST",
                dataType: "json",
                data: {
                    "personnage": personnage
                },
                async: true,
                success: function (data)
                {
                    console.log(data)
                }
            })
        });

    </script>

this is my html button :

<button id="wishlist" class="btn">add to wishlist</button>

what happens when i click on the button is i do get this error log :

1 AJAX request Method :POST Type:xhr Status:500 URL:/ajax_request

and the 'var_dump' in the controller dosen't display anything

Your mistake is on the route.

path(): Generates a relative URL path given a route name and parameters.

url() : Generates an absolute URL given a route name and parameters.

in this line use

url: "{{ url('ajax_route') }}",

Instead

url: "{{ path('ajax_route') }}",

and your controller

use Symfony\Component\HttpFoundation\JsonResponse;
*
*

      public function ajaxAction(Request $request)
         $arrayAjax = array("position" => "fasle");
        {
      if (($request->getMethod() == Request::METHOD_POST) && ($request->isXmlHttpRequest())) {

            $personnage = $request->request->get('personnage');
            dump($request->request);
            $wishlist = new wishlist();
            $wishlist->setUserid($personnage);
            $wishlist->setOfferid(1);
            $em=$this->getDoctrine()->getManager();
            $em->persist($wishlist);
            $em->flush();
            $arrayAjax = array("position" => "true");
         }
    return new JsonResponse($arrayAjax2);
       }