codeigniter +角JS:如何接收JSON数据 [英] Codeigniter + Angular Js: How to receive JSON data

查看:150
本文介绍了codeigniter +角JS:如何接收JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况是这样的:

我在角JS做了一个简单的应用程序,通过在codeigniter做出了API与服务器comunicate。

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.

这是code:

形式:

<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>

这是JS角控制器:

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

    });

}

codeigniter方法​​已经尝试了许多东西。
现在还有眼前这个:

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

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

但我不知道,因为它似乎是空的,如果可以接收数据到$ _ POST

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

所以,问题是:

如何在codeigniter方法​​接收数据?
不如送为JSON,然后json_de code?
我也曾尝试json_de code($ _ POST,真正的);
但为null。
但是,如果数据不在$ _ POST在哪里?
我有点糊涂了。

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..

感谢您的帮助!

编辑:

谢谢你们的答复。
这是一件事,都试过了。但不知何故,无法正常工作。
现在,例如方法是这样的:

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

但什么是返回一个布尔值false。

But what is returned is a boolean false.

推荐答案

感谢您的答复。
解决方法如下

thanks for the reply. solution is as follows

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

您可以通过

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

这篇关于codeigniter +角JS:如何接收JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆