Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
have you tried dumping $email and $password in PHP to see if you get the values? –  Tom Hart May 19 at 9:32
    
One of the main difficulties is the debug of the api.. Anyway if i dump $email i will then get it in the console and the result is false. –  johnnyfittizio May 19 at 10:38
    
How about putting $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

1 Answer 1

It seems to me you're posting an empty hash to the api. You set $scope.user to this :

   $scope.user = {}

and then do your request. You need to pass the user to your submit function somehow.

Besides, testing a boolean in success is not a best practice. Code your API so it sends an exception/error message on error cases, to be able to properly handle errors in your front-end code. See the relevant documentation, in general usage.

share|improve this answer
    
Thank you for reply. I have commented that line, but i am getting still the same issue –  johnnyfittizio May 19 at 11:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.