codeigniter 表单验证对于 json 数据总是错误的

标签 codeigniter rest

我正在使用 codeigniter 构建我自己的 REST Web 服务,该应用程序有一个名为“ 调用 ”的资源,可以通过 访问它。发布 方法,

调用 ” 函数通过 接收四个参数 [id, manager, department, level] 作为 json 对象。卷发使用此电话 内容类型:应用程序/json

然后我使用 codeigniter form_validation() 库来验证请求,然后如果 上没有错误,则返回响应数组form_validation()

问题是 form_validation() 总是返回 FALSE,尽管我可以按预期输出接收到的值。

下面是调用函数的代码

function calls_post() {

$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);
}
}

有什么建议吗? :)

最佳答案

http://ellislab.com/forums/viewthread/238080


$_POST = json_decode(file_get_contents("php://input"), true);
$this->form_validation->set_rules('id', 'id', 'required|numeric');
$this->form_validation->set_rules('department', 'department', 'required');

if($this->form_validation->run() == TRUE)
{   
    $id = $this->input->post('id'); 
    $department = html_escape($this->input->post('department'));    
    echo $id."<br>".$department;       
}
else
{   
    echo "false";        
}

关于codeigniter 表单验证对于 json 数据总是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18445681/

相关文章:

php - 查询依赖子查询太慢

javascript - 获取数组中字符串的最后一个字符

mysql - RedBean PHP信用卡号问题

php - 提供的参数无效 Codeigniter

mysql - 如何在mysql数据库中获取组的开始和结束预订日期?

javascript - 如何发送 REST 调用并返回 json 和结果?

javascript - ember.js rest JSON 递归嵌套在同一个模型中

java - eclipse 中的 jax-rs 2.0 要求

javascript - html改变div内容

php - 在 REST API 中的单个请求中插入或更新多个表中的记录是否合适?