i'm trying pass javascript objects view update controller in zend.
my json string looks :
[{"item_id":null,"parent_id":"none","depth":0,"left":"1","right":4},{"item_id":"1","parent_id":null,"depth":1,"left":2,"right":3}] and it's assigned variable jsonobj.
my ajax post looks :
$.ajax({ type: "post", url: "http://dev.jp-websolutions.co.uk/cms_nishan/admin/navigation/update", data: jsonobj, contenttype: "application/json; charset=utf-8", datatype: 'json', success: function(data) { alert(json.stringify(data, null, 4)); }, error: function() { alert("failure"); } }); return false; } ; and update controller :
public function updateaction() { if ($this->getrequest()->isxmlhttprequest()) { $this->_helper->layout()->disablelayout(); $this->_helper->viewrenderer->setnorender(); } $data = $this->_request->getpost(); $result = zend_json::decode($data); print_r($result); } but cant work, if use
$result = zend_json::decode([{"item_id":null,"parent_id":"none","depth":0,"left":"1","right":4},{"item_id":"1","parent_id":null,"depth":1,"left":2,"right":3}]); it displays properly,
array ( [0] => array ( [item_id] => [parent_id] => none [depth] => 0 [left] => 1 [right] => 4 ) [1] => array ( [item_id] => 1 [parent_id] => [depth] => 1 [left] => 2 [right] => 3 ) ) how can work? appreciated :)
you're sending json request body, no identifier, on php side need use getrawbody() json:
$data = $this->getrequest()->getrawbody(); the getpost() method should used if data submitted identifier contenttype application/x-www-form-urlencoded.
also make sure javascript variable jsonobj string containing json, not object. if it's object have jsonobj = json.stringify(jsonobj).
alternatively can send json identifier. change ajax to:
$.ajax({ type: "post", url: "http://dev.jp-websolutions.co.uk/cms_nishan/admin/navigation/update", data: {json : jsonobj}, datatype: 'json', success: function(data) { alert(json.stringify(data, null, 4)); }, error: function() { alert("failure"); } }); return false; }; on php side, use getpost('json'):
$data = $this->_request->getpost('json');
Comments
Post a Comment