$this->input->post() returns NULL in codeigniter rest api 实现

$this->input->post() returns NULL in codeigniter rest api implementation

在文件 RestClient.php 中,行 $uname = $this->input->post('username') 和 $pwd = $this->input->post('password') returns 空。

这是我的(视图)loginform.php

    <?php echo form_open('api/RestClient/PostCurl'); ?>
    <div class="form-group">
        <label for = "username"><h5>Username</h5>
            <input type = "text" name = "username" class="form-control" required
                   oninvalid = "this.setCustomValidity('username is required')"
                   oninput = "this.setCustomValidity('')" autocomplete="off"> <br>
        </label>
    </div>
    <div class="form-group">
        <label for = "password"><h5>Password</h5>
            <input type = "password" name = "password" class="form-control" required
                   oninvalid = "this.setCustomValidity('password is required')"
                   oninput = "this.setCustomValidity('')" autocomplete="off"> <br>
        </label>
    </div>
    <div class="form-group">
        <a href="<?php echo site_url('api/RestClient/PostCurl'); ?>"> <input id="loginsubmit" type = "submit" class="btn btn-success" class="form-control" value = "Login"/>
        </a>
    </div>

这是我的 RestServer API 控制器 - LoginFormApi.php

    class LoginFormApi extends REST_Controller {

public function __construct() {
    parent::__construct();
    $this->load->helper('url');
    $this->load->library('form_validation');
    $this->load->model('LoginFormModel');
    $this->load->model('EmployeeAccountModel');
}

public function LoginForm_post() {
    $userName = $this->post('username');
    $password = $this->post('password');
    $error = 'Invalid' . $userName;
    if (!$userName || !$password) {
        $this->set_response($error, REST_Controller::HTTP_NOT_FOUND);
    }
    $id = $this->LoginFormModel->Validate($userName, $password);
    if ($id) {
        $token['id'] = $id;
        $token['username'] = $userName;
        $date = new DateTime();
        $token['iat'] = $date->getTimestamp();
        $token['exp'] = $date->getTimestamp() + 60 * 60 * 5;
        $output['id_token'] = JWT::encode($token, "~~~JWT Auth Key !!!!!!~~~");
        $this->set_response($output, REST_Controller::HTTP_OK);
    } else {
        $this->set_response($error, REST_Controller::HTTP_NOT_FOUND);
    }
}

这是我使用 cURL 的客户端请求控制器 - RestClient.php 行 $uname = $this->input->post('username') 和 $pwd = $this->input->post('password') returns NULL.

    class RestClient extends CI_Controller{
public function __construct() {
    parent::__construct();
    $this->load->helper('url','form');
   $this->load->library('form_validation');
    $this->load->library('Curl');
}

public function PostCurl(){
    $username = 'admin';
    $password = 'apitest@1234';
    $uname = $this->input->post('username');
    $pwd = $this->input->post('password');
    echo '~'.$uname.$pwd.'~';
    $curl_handle = curl_init('http://localhost:8085/ci-api/index.php/api/LoginFormApi/LoginForm');
    curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
    $dataPost = array('username' => $uname,'password' => $pwd);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $dataPost);
    curl_setopt($curl_handle, CURLOPT_USERPWD, $username.':'.$password);
    $data = curl_exec($curl_handle);
    curl_close($curl_handle);
    $result = json_decode($data);
    if(isset($result->status) && $result->status === 'success'){
        echo 'success' .$result;
    }else{
        $post = var_dump($dataPost);
        echo $post.'failure'.$result;
    }
}

}

我在视图中尝试了以下 js 片段并且它起作用了,post 数据在 RestClient.php 控制器中被访问:

    <script>
        function FormData(){
            var formData = {};
            formData.username = $('#username').val();
            formData.password = $('#password').val();
            $.ajax({
               url: 'http://localhost:8085/ci-api/index.php/api/RestClient/PostCurl',
               type: 'post',
               dataType: 'json',
               data: JSON.stringify(formData),
               ContentType: 'application/json'
            });
        }
    </script>