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

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

问题描述

情况是这样的:

我有一个用 Angular JS 制作的简单应用程序,它通过在 codeigniter 中制作的 API 与服务器进行通信.

应用中有登录系统.当用户输入邮箱和密码时,这些数据被发送到服务器,如果邮箱存在并且密码匹配,则返回true.

我做了很多尝试,但不知道如何正确地做到这一点.

这是代码:

表格:

<div class="form-group"><label for="exampleInputEmail1">电子邮件地址</label><input type="email" class="form-control" name="email" ng-model="user.name" placeholder="输入电子邮件">

<div class="form-group"><label for="exampleInputPassword1">密码</label><input type="password" class="form-control" name="password" ng-model="user.password" placeholder="密码">

<button type="submit" class="btn btn-default">提交</button></表单>

这是 Angular js 控制器:

 $scope.authorized = false;$scope.user = {};$scope.submitForm = function(){console.log("发布数据....");$http({方法:'POST',网址:'http://127.0.0.1/api/main/login',标头:{'内容类型':'应用程序/json'},数据:JSON.stringify({email:$scope.user.email, password:$scope.user.password})}).成功(功能(数据){控制台日志(数据);$scope.authorized = 数据;if ($scope.authorized) { $location.path("memberArea");};});}

codeigniter 方法中尝试了很多东西.现在只有这个:

函数登录(){打印 json_encode($_POST);}

但是我不知道是否可以将数据接收到 $_POST 中,因为它似乎是空的.

问题是:

如何在 codeigniter 方法中接收数据?最好先以 JSON 格式发送,然后再以 json_decode 格式发送?我也试过 json_decode($_POST, true);但为空.但是如果数据不在 $_POST 中呢?我有点糊涂了..

感谢您的帮助!

谢谢大家的回复.那是已经尝试过的一件事.但不知何故不起作用.现在例如方法是这样的:

函数登录(){$email = $this->input->post('email');var_dump($email);打印 json_encode($email);}

但返回的是布尔值 false.

解决方案

感谢您的回复.解决方法如下

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

您可以通过以下方式进行测试

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

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.

解决方案

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

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

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆