This is the situation:
I have a simple application made in Angular JS that communicates with the server through an API created in the codec.
The application has a login system. When the user enters an email address and password, this data is sent to the server, if there is mail and the password matches, it returns true.
I made many attempts, but did not understand how I can do this correctly.
This is the code:
The form:
<form role="form" method="post" novalidate ng-submit="submitForm()"> <div class="form-group"> <label for="exampleInputEmail1">Email address</label> <input type="email" class="form-control" name="email" ng-model="user.name" placeholder="Enter email"> </div> <div class="form-group"> <label for="exampleInputPassword1">Password</label> <input type="password" class="form-control" name="password" ng-model="user.password" placeholder="Password"> </div> <button type="submit" class="btn btn-default">Submit</button> </form>
This is the Angular js controller:
$scope.authorized = false; $scope.user = {}; $scope.submitForm = function() { console.log("posting data...."); $http({ method : 'POST', url : 'http://127.0.0.1/api/main/login', headers: {'Content-Type': 'application/json'}, data : JSON.stringify({email:$scope.user.email, password:$scope.user.password}) }).success(function(data) { console.log(data); $scope.authorized = data; if ($scope.authorized) { $location.path("memberArea"); }; }); }
We tried a lot in the codeigniter method. Now there is only the following:
function login() { print json_encode($_POST); }
But I do not know if it can receive data in $ _POST, because it seems empty.
So the question is:
How can I get the data in the codeigniter method? Better to send as JSON and then json_decode? I also tried json_decode ($ _ POST, true); But it was zero. But if the data is not in $ _POST, where? I'm a little confused.
Thank you for your help!
EDIT:
Thanks guys for the answer. It was one that they tried. But somehow it doesnβt work. Now, for example, the method looks like this:
function login() { $email = $this->input->post('email'); var_dump($email); print json_encode($email); }
But what comes back is logical false.
json angularjs post php codeigniter
johnnyfittizio
source share