Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

php - Codeigniter + Angular Js: How to receive JSON data

This is the situation:

I have a simple app made in Angular JS that comunicate with the server through an API made in codeigniter.

There is a login system in the app. When the user enter email and password, this data are sent to the server, if the email exist and the password match, it return true.

I have made many attempts but not figure out how can i do this properly.

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"); };        

    });

}

In the codeigniter method have tried many things. Right now there is just this:

function login()
{
    print json_encode($_POST);
}

But i don't know if can receive the data into the $_POST because it seems to be empty.

So the question is:

How can i receive data in the codeigniter method? Is better to send as JSON and then json_decode? I have also tried json_decode($_POST, true); But was null. But if the data are not in $_POST where are? I am little confused..

Thank you for help!

EDIT:

Thanks guys for reply. That was one thing that have tried. But somehow is not working. Now for example the method is like this:

function login()
{
    $email = $this->input->post('email');
    var_dump($email);
    print json_encode($email);
}

But what is returned is a boolean false.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

thanks for the reply. solution is as follows

$obj=json_decode(file_get_contents('php://input'));

you can test it by

print_r(json_decode(file_get_contents('php://input')));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...