I need to store some map parameter in my symfony project, to do this i need to implement some Ajax in my view which will be able to pass some info to the controller.
I read the docs, try to write some code but i can't make it works. And Ajax is really painfull to debug. Here is the controller part :
/**
* @Route("/ajax", name="_recherche_ajax")
*/
public function ajaxAction()
{
$isAjax = $this->get('Request')->isXMLHttpRequest();
if ($isAjax) {
return new Response('This is ajax response');
}
return new Response('This is not ajax!', 400);
}
And the JS :
map.on('zoomend', function(e) {
// use callback e variable
console.log('zoom: ' + e.target.getZoom());
$.ajax({
type: "POST",
url: "/recherche/ajax",
data: {
zoom: e.target.getZoom()
},
dataType: "json",
success: function(response) {
console.log(response);
}
});
});
I check the url recherche/ajax
it does exist and return the 'This is not Ajax' as expected. But the console.log does not return any value...
Is that the right way to do this ?
EDIT : It looks like the controller can't handle POST Request. I tried to modify the annotations to :
/**
* @Route("/ajax", name="_recherche_ajax")
* @Method({"GET", "POST"})
*/
But it returns :
([Semantical Error] The annotation "@Method" in method MySite\SiteBundle\Controller\RechercheController::ajaxAction() was never imported. Did you maybe forget to add a "use" statement for this annotation?)