This is the situation:
I have a simple Angular App that should send some data to API. This data are the email and password taken from a form. Then the API should check if user exist and return true or false.
I am not able to send properly the data to the API.
This is the code: The Controller:
.controller('LoginCtrl', function($scope,$http,$location) {
$scope.submit = function()
{
console.log('form submitting..');
$scope.user = {}
$http({
method : 'POST',
url : 'http://localhost/myAppAPI/main/login',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $scope.user
}).success(function(data) {
console.log('data(from API): ' + data);
console.log('user: ' + $scope.user);
$scope.authorized = data;
console.log('authorized: ' + $scope.authorized);
if ($scope.authorized == "true")
{
$location.path("/tab/account");
} else
{
$scope.message = "You are not authorized";
}
});
}
})
The form:
<form role="form" method="post" ng-submit="submit()" novalidate>
<div class="list">
<label class="item item-input item-stacked-label">
<span class="input-label">Email</span>
<input type="text" ng-model="user.email" name="email" placeholder="[email protected]">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Password</span>
<input type="password" ng-model="user.password" name="password" placeholder="Your password">
</label>
</div>
<input type="submit" id="submit" value="Login" class="button button-block button-positive"/>
</form>
This is the API, made in Codeigniter:
$email = $this->input->post('email');
$password = $this->input->post('password');
if ($email == "test") {
$data = true;
}else {
$data = false;
}
echo json_encode($data);
This is the printing from the console:
user: [object Object]
data(from API): false
authorized: false
So probably the problem can be the the object user i am sending is actually empty. But i should be able to retrieve them using $this->input->post() right?
This is the question:
How can i propely send data to API ın Angular Js using $http ?
Thank you!
$email
and$password
in the array you return so you can see the values? Or look at FirePHP, a plugin for Firebug/Firefox that lets you safely dump variables from PHP without breaking ajax calls. But I do believe Pak's answer will solve your problem. – Tom Hart May 19 at 10:56