在文件RestClient.php中,行$ uname = $ this-> input-> post('username')和$ pwd = $ this-> input-> post('password')返回NULL。
这是我的(查看)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')返回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;
}
}
}
答案 0 :(得分:0)
我在视图中尝试了以下js代码段,它起作用了,可以在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>