Symfony 4 and Leaflet

I developed a Symfony application with Leaflet and I placed names in the database.

I created a search bar to show a place typed by the user.

I collected the data by ajax but I don’t know where is the problem because when I make a console.log it shows nothing.

My controller code:

    /**
     * @Route("/map",name="map_index",methods={"GET","POST"})
     *
     */

    public function indexMap(Request $request, ElementRepository $repo)
    {
        // $search = new SearchElement();
        $searchform = $this->createForm(MapSearchType::class);
        $searchform->handleRequest($request);
        $donnes = $repo->findAll();
        $data = array();
        $response = new JsonResponse();
        if ($searchform->isSubmitted() && $request->isXmlHttpRequest()) {
           // $name = $request->get('name');
            $name = $searchform->getData()->getName();
            $donnes = $repo->search($name);
            // dump($donnes);
            if ($donnes) {
                foreach ($donnes as $r) {
                    $data = array(
                        'id' => $r->getId(),
                        'name' => $r->getName(),
                        'lat' => $r->getLatitude(),
                        'lon' => $r->getLongitude(),
                        //  'type' => $r->getRfType()->getLibelle(),
                        'category' => $r->getCategory()->getNameCat()
                    );
                }
            }
            dump($data);
            $response->setData($data);
            return $response;
        }



        return $this->render('map/index.html.twig', [
            'form'    => $searchform->createView(),
            'data' => $data
        ]);
    }

My ajax code:

$('.js_search').on('click',function(event){

         $.ajax({
            url: "{{ path('map_index') }}",
            method: "GET",
            succes: function (data) {
                console.log(data);
                alert("the data:" + data);
            },
            error:
            function(){
                alert("oups error");
            }

        });
});

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *