【问题标题】:I can't validate json object to POST on codeigniter我无法验证 json 对象以在 codeigniter 上发布
【发布时间】:2013-08-26 21:38:58
【问题描述】:

在我的 REST 应用程序中,我尝试将 json 对象发送到我的 API 服务器,并且服务器必须验证数据以确保每件事都按预期进行。

在 API 服务器上进行验证的控制器

$this->load->model('api/central');
$this->load->library('form_validation');

//validation rules
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('id', 'id', 'required|numeric');
$this->form_validation->set_rules('manager', 'manager', 'required|numeric');
$this->form_validation->set_rules('department', 'department', 'required');
$this->form_validation->set_rules('level', 'level', 'required|numeric');


if ($this->form_validation->run() === FALSE) {

    $employeeId = $this->form_validation->error('id') ? $this->form_validation->error('id') : $this->post('id');
    $manager = $this->form_validation->error('manager') ? $this->form_validation->error('manager') : $this->post('manager');
    $department = $this->form_validation->error('department') ? $this->form_validation->error('department') : $this->post('department');
    $level = $this->form_validation->error('level') ? $this->form_validation->error('level') : $this->post('level');

    $response = array(
        'status' => FALSE,
        'error' => 'We don\'t have data to display, Minimum information required to process the request is missing',                
        'authenticated' => TRUE,                
        'id' => $employeeId,
        'manager' => $manager,
        'department' => $department,
        'level' => $level                
    );

    $this->response($response, 200);

} else {

    $response = $this->central->calls_get($this->post('id'), $this->post('manager'), $this->post('department'), $this->post('level'));
    $this->response($response, 200);
}

$this->post('id'), $this->post('manager'), $this->post('department'), $this->post('level')返回提交的值,但是form_validation库虽然按预期发送,但始终无法验证数据的问题

以下是一些有用的 http 信息 http-header ('content-type') = 'application/json' 以及通过 post 作为 json 对象发送的数据,例如:{"id":"1", "manager": "1", "department": “销售额”,“级别”:“1”}

【问题讨论】:

    标签: php json validation rest codeigniter-2


    【解决方案1】:

    我看不出有什么问题,但我可以在调试时提出建议,如果你打电话给:

    echo validation_errors();
    

    它经常给你一些有用的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-20
      • 1970-01-01
      • 2022-01-17
      • 2021-07-22
      • 2011-04-23
      • 1970-01-01
      • 2013-04-10
      • 2020-11-25
      相关资源
      最近更新 更多