我尝试与$this->post()
获取以 json 格式通过 post 发送的数据。例如,我无法得到任何结果$this->post('name')
.
这是代码:
<?php
require(APPPATH . '/libraries/REST_Controller.php');
class user_api extends REST_Controller {
function user_post() {
$user = array(
'id' => null,
'firstname' => $this->post('firstname'),
'lastname' => $this->post('lastname')
);
$result = $this->user_model->insert($user);
if ($result) {
$this->response($result, 200); // 200 being the HTTP response code
} else {
$this->response(NULL, 404);
}
}
}
?>
数据以 json 格式发送到链接:http://mydomain.com/myapplication/user_api/user http://mydomain.com/myapplication/user_api/user :
{"firstname":"test",
"lastname":"test"
}
数据库中没有数据,无法从中获取数据$this->post('firstname')
.
任何想法 ????
你无法通过 post 方法获取 json 数据,你必须像这样使用
require(APPPATH . '/libraries/REST_Controller.php');
class user_api extends REST_Controller {
function user_post() {
$params = json_decode(file_get_contents('php://input'), TRUE);
$user = array(
'id' => null,
'firstname' => $params['firstname'],
'lastname' => $params['lastname']
);
$result = $this->user_model->insert($user);
if ($result) {
$this->response($result, 200); // 200 being the HTTP response code
} else {
$this->response(NULL, 404);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)