AngularJS未发布到Codeigniter控制器

时间:2013-08-30 23:56:39

标签: php javascript codeigniter angularjs

我正在使用Codeigniter和AngularJS。当我使用方法和操作提交login.php时,Ci控制器成功地提取后期数据。但是,当我尝试并使用角度提交表单时,它会中断。运行print_r时,我什么也得不到。 app.js脚本甚至成功地从cred中提取值,而不是将其发布到Ci控制器。有什么想法吗?

的login.php

<form ng-submit="login()">
  <fieldset>
    <legend>Login</legend>
    <label>Type your username and password</label>
    <input autofocus ng-model="cred.username" type="text" placeholder="Type your username" required><br>
    <input ng-model="cred.password" type="password" placeholder="Type your password" required><br>
    <button type="submit" class="btn">Submit</button>
  </fieldset>
</form>

app.js

app.factory("AuthenticationService", function($http, $location) {
    return {
        login: function(cred) {
            return $http({
                method : 'POST',
                url : 'index.php/home/login',
                data : cred
            });
        },
    };
});

app.controller('LoginController', function($scope, AuthenticationService) {
    $scope.cred = { username: "", password: ""};
    $scope.login = function() {
        AuthenticationService.login($scope.cred);
    };
});

CodeIgniter控制器

public function login() {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $this->db->where('username', $username);
        $this->db->where('password', $password);
        $query = $this->db->get('users');
        print_r($_POST);
            if ($query->num_rows() > 0) {
                $row = $query->row();
                echo json_encode(array('isSuccessful' => true));
            } else {
                echo json_encode(array('flash' => 'Invalid username or password'));
            }
    }

1 个答案:

答案 0 :(得分:4)

默认情况下,它可能会将表单提交为“请求有效负载”,这会让CodeIgniter感到困惑。看看How can I post data as form data instead of a request payload?如何解决它。