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

How to Receive JSON sent from client side to server Side in cakePHP 3.x

我收到的是字符串而不是 Json。我正在访问 received 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.");
    }  
}

查看文档,解释得很好。 Request::input() 默认 returns 原始字符串格式的数据,如果需要转换,将回调传递给方法进行处理,例如 json_decode.

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

另见

试试这个

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

或者将输入保存到不同的变量,然后尝试如下保存该变量

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