如果正在向我发送HTTP请求,而其中HTTP请求的主体只是一个UTF8编码的字符串,如何在我的cakephp Controller 中访问该数据?看来$ this-> params仅包含以下内容:
{
"pass":[],
"named":[],
"controller":"users",
"action":"checkin",
"plugin":null,
"url":{
"ext":"json",
"url":"users\/checkin.json"
},
"form":[],
"isAjax":false
}
所发布的数据如下所示:
{
"sessionkey":"somecrazykey",
"longitude":"-111.12345",
"latitude":"33.12345",
"reqtype":"checkin",
"location":"the mall",
"public":"true"
}
最佳答案
if($this->RequestHandler->requestedWith('json')) {
if(function_exists('json_decode')) {
$jsonData = json_decode(utf8_encode(trim(file_get_contents('php://input'))), true);
}
if(!is_null($jsonData) and $jsonData !== false) {
$this->data = $jsonData;
}
}
这是一个提议作为核心的代码段,请参见https://trac.cakephp.org/ticket/6125。也许这就是您要寻找的。
-比约恩
关于cakephp - 如何处理以HTTP Post形式发送到cakephp应用程序的json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1766621/