I need to pass json data to my Symfony Controller. My ajax function looks like this:
var data = '{"firstname":"John"}';
$.ajax({
type: "POST",
url: save_url, //path to controller action
data: {json:data},
success: function(response) {
// Do something
}
});
In my controller, I try to get my data through:
public function createAction(Request $request) {
$data = $this->getRequest()->get('firstname');
return $this->render('MyBundle:Counter:test.html.twig', array(
'data' => $data
));
Just to see if this works, I send $data
to be echoed in a template. In Firebug I can see the data being sent and everything seems to work, but $data
is empty and nothing is echoed. Where am I doing this wrong?
EDIT: When I check the response in Fireburg
console, I see my data there, in place, but it never appears in the template. var_dump($data)
tells that $data
is null
. So, it seems data is being sent but the controller ignores it.