如何在cakePHP 3.x中接收从客户端发送到服务器端的JSON

时间:2015-05-29 15:21:46

标签: cakephp cakephp-3.0

我收到一个字符串而不是Json。我正在访问收到的json使用 $this->request->input()

我的添加方法如下:

function add()
{
    if (!empty($this->request->input())) {  
        $this->loadModel('crud');
        if($this->crud->save( $this->request->input() ) )
        {  
            $this->Flash->set("Operation Completed.");
            $this->set('message',"Your user data has been saved.");
        }
        else
            $this->set('message',"Error.");
    }  
}

2 个答案:

答案 0 :(得分:0)

检查文档,这一切都得到了很好的解释。默认情况下,Request::input()以原始字符串格式返回数据,如果需要将其转换,请将回调传递给方法来处理,例如json_decode

$this->request->input('json_decode');

另见

答案 1 :(得分:0)

试试这个

$this->crud->save( json_decode($this->request->input()))

或将输入保存到其他变量,然后尝试保存该变量,如下所示

$input = json_decode($this->request->input())
$this->crud->save($input)