douwen5066 2013-07-19 11:45
浏览 31

Ajax POST Javascript对象到Zend控制器和接收

I'm trying to pass javascript objects from view to update controller in Zend.

My JSON string looks like :

[{"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 to variable jsonObj.

My AJAX post looks like :

$.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 my update controller is :

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 I cant get it to work, if I 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, as

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 I get this work? Any help will be much appreciated :)

  • 写回答

2条回答 默认 最新

  • dongweiben5229 2013-07-19 12:07
    关注

    You're sending JSON as the request body, with no identifier, so on the PHP side you need to use getRawBody() to get the JSON:

    $data = $this->getRequest()->getRawBody();
    

    The getPost() method should only be used if the data was submitted with an identifier with contentType application/x-www-form-urlencoded.

    Also make sure that your Javascript variable jsonObj is a string containing the JSON, not an object. If it's an object you will have to jsonObj = JSON.stringify(jsonObj).

    Docs for Zend Request Object


    Alternatively you can send the JSON with an identifier. Change the 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 the PHP side, use getPost('json'):

    $data = $this->_request->getPost('json');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据