php - Sending json to symfony controller -
i need pass json data symfony controller. ajax function looks this:
var data = '{"firstname":"john"}'; $.ajax({ type: "post", url: save_url, //path controller action data: {json:data}, success: function(response) { // } });
in controller, try data through:
public function createaction(request $request) { $data = $this->getrequest()->get('firstname'); return $this->render('mybundle:counter:test.html.twig', array( 'data' => $data ));
just see if works, send $data echoed in template. in firebug can see data being sent , seems work, $data empty , nothing echoed. doing wrong?
edit: when check response in fireburg console, se data there, in place, never appears in template. var_dump($data) tells $data null. so, seems data being sent controller ignores it.
as marek noticed:
$this->getrequest()
already returns request object, you're accessing request
property of request, doesn't add up. either try:
$data = $this->request->get('json');
or use:
$data = $this->getrequest()->get('json');
you can, of course assign return value of $this->getrequest()
variable, , call get
method on var there on end... anyway, here's initial answer, contain more tips, , considerations may find useful:
you should able data way, though ajax requests + echoing in template? sound bit strange. don't see passing $data
variable $this->render
call anywhere.
this copy-paste bit controller action in 1 of projects. works fine there:
public function indexaction() { if (!$this->getrequest()->isxmlhttprequest()) {//check if request ajax request, if not redirect return $this->redirect( $this->generateurl('foo_bar_homepage')//changed this, of course ); } $id = $this->getrequest()->get('id',false);//works fine
however, can't begin grasp why you're doing this:
var data = '{"firstname":"john"}';
why not go for:
$.ajax({ type: "post", url: url,//post how url please... data: {firstname: 'john'},//jq sort out success: function(response) { console.log(response); } error: function() { console.log('an error occured'); console.log(arguments);//get debugging! } });
then, in controller you're able to:
$this->getrequest()->get('firstname');//it should john
you pass {json:{firstname: 'john'}}
data param $.ajax
, difference in controller be, have this:
$data = $this->getrequest()->get('json'); $firstname = $data['firstname'];
that should work fine, unless there's somthing you're not telling :)
recap:
i'd write:
public function createaction() {//no request param in controller if (!$this->getrequest()->isxmlhttprequest()) {//no ajax request, no play... $this->redirect( $this->generateurl('homepage_route') ); } $data = $this->getrequest()->get('firstname'); //return json response: return new response(json_encode(array('datareceived' => $data)); //return rendered html page: return $this->render('mybundle:counter:test.html.twig', array( 'data' => $data )); }
of course, js code should read:
$.ajax({ type: "post", url: 'route/to/create' data: {firstname:'john'}, success: function(response) { console.log(response); } });
i have tested this, , see no reason why shouldn't work. works fine me...
Comments
Post a Comment