你好,我用 API 做了一个 post 方法,我想做出如下回应,什么可以?
{
"status": 200,
"error": false,
"data": [
{
"id_kab": "56",
"id_prov": "1",
"kd_kab": "CGK10000"
}
]
}
我通过邮递员所做的现在结果是这样的
{
"id_kab": "56",
"id_prov": "1",
"kd_kab": "CGK10000"
}
这是我的代码插入到数据库
public function submit_post() {
$data = array(
'id_kab' => $this->input->post('id_kab'),
'id_prov' => $this->input->post('id_prov'),
'kd_kab' => $this->input->post('kd_kab')
);
$insert = $this->db->insert('service', $data);
if ($insert) {
$arr=array(
'status' => 200,
'message' => 'Success'
);
header('Content-Type: application/json');
echo json_encode($arr,TRUE);
} else {
$this->response(array('status' => 'fail', 502));
}
}
如何更改响应?谢谢
从给出的响应来看,数据应该以数组的形式给出,所以请尝试以下代码
$output = array();
$data = array();
$data[] = array(
'id_kab' => $this->input->post('id_kab'),
'id_prov' => $this->input->post('id_prov'),
'kd_kab' => $this->input->post('kd_kab')
);
$output["data"] = $data;
$output["status"] = 200;
$output["error"] = false;
echo json_encode($output); exit;
输出如下
{
"status": 200,
"error": false,
"data": [{
"id_kab": "56",
"id_prov": "1",
"kd_kab": "CGK10000"
}]
}
演示