javascript - Send ajax data to php controller in symfony2 -


hi guys new in symfony 2 , have little confusing sending data ajax php controller in symfony 2. want create project google map create mapcontroller:

   <?php  namespace appbundle\controller;  use sensio\bundle\frameworkextrabundle\configuration\route; use symfony\bundle\frameworkbundle\controller\controller; use symfony\component\httpfoundation\response; use symfony\component\httpfoundation\request; use symfony\component\httpfoundation\jsonresponse; class mapcontroller extends controller {      /**      * @route("/", name="homepage")      */     public function indexaction(request $request)     {         // replace example code whatever need         return $this->render('map/map.html.twig', [             'base_dir' => realpath($this->getparameter('kernel.root_dir').'/..').directory_separator,             'contdata' => ['lat' => '51.24591334500776', 'lng'=> '22.56967306137085']         ]);     }       public function saveaction(request $request)     {         $data = $request->request->get('params');           return new jsonresponse(['data' => $data], response::http_ok);     }  } 

then create routing:

    app:     resource: '@appbundle/controller/'     type: annotation map:     path:      /map     defaults:  { _controller: appbundle:map:index }     requirements:         page: '\d+'  map_save:     path:      /map/save     defaults:  { _controller: appbundle:map:save }     methods:  [post]  

so when go to route:

http://localhost/googlemap/web/app_dev.php/map

i display template map.html.twig

there have javascipt function tried send ajax data controller:

    marker.addlistener("click", function () {                       //!                     var http = new xmlhttprequest();                     var url = "map/save";                     var params = marker.position.lat();                     http.open("post", url, true);  //send proper header information along request                     http.setrequestheader("content-type", "application/x-www-form-urlencoded");                      http.onreadystatechange = function () {//call function when state changes.                         if (http.readystate == 4 && http.status == 200) {                             alert(http.responsetext);                         }                     }                     http.send(params); 

but have null in response: {"data":null}

you need ask want data sent js. if it's related map feature creating new method in mapcontroller seems fine. if it's not related maps creating new controller way go.

naming of method , controller should relevant you're doing. savedata example not obvious. if you're saving coordinates name method savecoordinatesaction , define dedicated route supporting post requests only.

as passing url js, check out fosjsroutingbundle - lets generate specific routes directly javascript.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -