我已经在互联网上搜索了如何$http
在 codeigniter 中形成 Angular js POST 请求发送的验证数据。
为了清楚地理解我已经发布了完整的 HTML 数据。我相信大多数开发人员都在寻找这个解决方案。
这是我的 HTML 表单:
<!DOCTYPE html>
<html>
<head>
<style>
input[type="text"].ng-invalid{border: 1px solid red; }
</style>
</head>
<body ng-app="app" ng-controller="ctrl">
<form name="test_form" ng-submit="send_data()">
<input type="text" name="email" ng-model="email">
<span ng-show="test_form.email.$invalid - required">Required</span>
<input type="password" name="password" ng-model="password">
<span ng-show="test_form.email.$invalid - required">Required</span>
<button type="submit">Submit</button>
</form>
<script src="<?php echo base_url('assets/angular/angular.min.js'); ?>" type="text/javascript">
</script>
<script>
angular.module('app', [])
.controller('ctrl', function ($scope, $http) {
$scope.send_data = function () {
$http({
method: "POST",
url: "<?php echo base_url('login/test_angular_validate'); ?>",
data: {email: $scope.email, password: $scope.password},
headers : {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function (success) {
console.log(success);
}, function (error) {
})
}
});
</script>
</body>
</html>
后端 Codeigniter 登录控制器功能
<?php
public function test_angular_validate() {
$form_data = json_decode(file_get_contents("php://input"), true);
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'email', 'required|min_length[3]');
$this->form_validation->set_rules('password', 'password', 'required');
if ($this->form_validation->run() == FALSE) {
echo 'failed';
print_r(validation_errors());
} else {
echo 'success';
}
}
?>
何时使用角度$http
POST 请求发送 html 表单数据我无法使用 codeigniter 表单验证库验证该数据。它会引发 此图像中给出的验证错误。