如何将json类型数组$ .post发送到phalcon

时间:2017-02-22 14:44:43

标签: jquery post phalcon

我正在尝试发送一个数据类型为array []的表单 我在phalcon控制器上处理它们时遇到了麻烦

  <form id="form_access" name="form_access" >
  <table class="table table-hover">

                <tr>
                    <td>
                        <input type='text' name="codigo[]" id="codigo[]" value="100" />                      
                    </td>
                    <td>
                        <input type="text" name="access[]" id="access[]"  value="1" />
                    </td>
                    <td>
                        <input type="text" name="insert[]" id="insert[]"  value="1" />
                    </td>

                    <td>
                        <input type="text" name="edit[]" id="edit[]"  value="1" />
                    </td>
                    <td>
                        <input type="text" name="delete[]" id="delete[]"  value="1" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type='text' name="codigo[]" id="codigo[]" value="101" />                      
                    </td>
                    <td>
                        <input type="text" name="access[]" id="access[]"  value="1" />
                    </td>
                    <td>
                        <input type="text" name="insert[]" id="insert[]"  value="1" />
                    </td>

                    <td>
                        <input type="text" name="edit[]" id="edit[]"  value="1" />
                    </td>
                    <td>
                        <input type="text" name="delete[]" id="delete[]"  value="1" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input type='text' name="codigo[]" id="codigo[]" value="102" />                      
                    </td>
                    <td>
                        <input type="text" name="access[]" id="access[]"  value="1" />
                    </td>
                    <td>
                        <input type="text" name="insert[]" id="insert[]"  value="1" />
                    </td>

                    <td>
                        <input type="text" name="edit[]" id="edit[]"  value="1" />
                    </td>
                    <td>
                        <input type="text" name="delete[]" id="delete[]"  value="1" />
                    </td>
                </tr>

 </table>
 </form>

这是我的jquery

    str = $("#form_access").serialize();

    $.post("access/save",str, function(data){  
        console.log(data);
    },'json');

这是我的控制器

 public function saveAction()
{

    $this->view->disable();

    if ($this->request->isPost()) {


        $codigo = $this->request->getPost("codigo");
        $access = $this->request->getPost("access");
        $insert = $this->request->getPost("insert");
        $edit = $this->request->getPost("edit");
        $delete = $this->request->getPost("delete");

        $data = array();

        foreach ($codigo as $key => $value)
        {
            $data[] = ($value." - ".$access[$key]." - ".$insert[$key]." - ".$edit[$key]." - ".$delete[$key]);
        }

        $this->response->setJsonContent(array('res' => $data));
        $this->response->setStatusCode(200, "OK");
        $this->response->send();
    }

    $this->response->setStatusCode(400, "NOT FOUND");
    $this->response->send();

}

但是在我的控制台中我得到了这个

{"res":["100 -  -  -  - ","101 -  -  -  - ","102 -  -  -  - "]}

显然我只是从第一个foreach获取数据。

我做错了什么,请帮忙。

2 个答案:

答案 0 :(得分:0)

$.ajax({
  type: "POST",
  url: "access/save",
  data: str,
  success: success,
  dataType: 'json'
});

docs

中的更多信息

答案 1 :(得分:0)

**行动**

   $request = new \Phalcon\Http\Request();
    if ($this->request->isPost()) {

        $this->view->disable();
        if ($request->isAjax()) {

            $codigo = $this->request->getPost("codigo");
            $access = $this->request->getPost("access");
            $insert = $this->request->getPost("insert");
            $edit = $this->request->getPost("edit");
            $delete = $this->request->getPost("delete");
            $data = array();
            foreach ($codigo as $key => $value) {
                $data[] = ($value . " - " . $access[$key] . " - " . $insert[$key] . " - " . $edit[$key] . " - " . $delete[$key]);
            }

            $this->response->setJsonContent(array('res' => $data));
            $this->response->setStatusCode(200, "OK");
            $this->response->send();
        }

    }else{
        $this->response->setStatusCode(400, "NOT FOUND");
        $this->response->send();
    }

<强>的Javascript

$(document).ready(function () {
    // process the form
    str = $("#form_access").serialize();
       $.post("/access/save", str, function (data) {
           console.log(data);
       }, 'json');

});

结果

Object
res:Array[3]
0:"100 - 1 - 1 - 1 - 1"
1:"101 - 1 - 1 - 1 - 1"
2:"102 - 1 - 1 - 1 - 1"
length:3