Codeigniter + Angular Js: How to receive JSON data

时间:2022-10-06 13:40:27

This is the situation:

情况就是这样:

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

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

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.

应用程序中有一个登录系统。当用户输入电子邮件和密码时,此数据将发送到服务器,如果电子邮件存在且密码匹配,则返回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:

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

    });

}

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

在codeigniter方法中尝试了很多东西。现在有这样的:

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.

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

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

如何在codeigniter方法中接收数据?最好发送为JSON,然后发送json_decode?我也试过json_decode($ _ POST,true);但是没有。但是如果数据不在$ _POST里面呢?我有点困惑..

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.

但返回的是布尔值false。

5 个解决方案

#1


12  

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

#2


3  

$_POST will be empty in CodeIgniter because it purposely empties it for security reasons. You need to use $this->input->post(); instead.

CodeIgniter中的$ _POST将为空,因为出于安全原因,它会故意清空它。你需要使用$ this-> input-> post();代替。

CodeIgniter Input Class

CodeIgniter输入类

#3


2  

Use:

使用:

$postData = $this->input->post();

It should give you an array with all the post data.

它应该给你一个包含所有发布数据的数组。

I also advise you to turn on XSS filtering.

我还建议你打开XSS过滤。

Following is the documentation for the Codeigniter Input Class: http://ellislab.com/codeigniter/user-guide/libraries/input.html

以下是Codeigniter输入类的文档:http://ellislab.com/codeigniter/user-guide/libraries/input.html

#4


1  

the data sent with the request is a name-value pairs so you should write some thing like that:

与请求一起发送的数据是一个名称 - 值对,所以你应该写一些这样的东西:

data : {"myformdata":JSON.stringify({email:$scope.user.email, password:$scope.user.password})}

and in code igniter you can recive the data :

在代码点火器中,您可以重新获取数据:

$this->input->post("myformdata"); //should return what
                                  // that JSON.stringify returned

#5


1  

Although this is already answered I quite often have this quick little utility included in many of my applications that need to accept both application/x-www-form-urlencoded and application/json POSTs.

虽然这已经得到解答,但我经常在我的许多应用程序中包含这个快速的小实用程序,它需要同时接受application / x-www-form-urlencoded和application / json POST。

if (strcasecmp($_SERVER['REQUEST_METHOD'], 'post') === 0 && stripos($_SERVER['CONTENT_TYPE'], 'application/json') !== FALSE) {
    // POST is actually in json format, do an internal translation
    $_POST += json_decode(file_get_contents('php://input'), true);
}

After that point you can now just use the $_POST superglobal as you normally would, all the JSON data will be decoded for you.

在此之后,您现在可以像往常一样使用$ _POST超全局,所有JSON数据都将为您解码。

#1


12  

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

#2


3  

$_POST will be empty in CodeIgniter because it purposely empties it for security reasons. You need to use $this->input->post(); instead.

CodeIgniter中的$ _POST将为空,因为出于安全原因,它会故意清空它。你需要使用$ this-> input-> post();代替。

CodeIgniter Input Class

CodeIgniter输入类

#3


2  

Use:

使用:

$postData = $this->input->post();

It should give you an array with all the post data.

它应该给你一个包含所有发布数据的数组。

I also advise you to turn on XSS filtering.

我还建议你打开XSS过滤。

Following is the documentation for the Codeigniter Input Class: http://ellislab.com/codeigniter/user-guide/libraries/input.html

以下是Codeigniter输入类的文档:http://ellislab.com/codeigniter/user-guide/libraries/input.html

#4


1  

the data sent with the request is a name-value pairs so you should write some thing like that:

与请求一起发送的数据是一个名称 - 值对,所以你应该写一些这样的东西:

data : {"myformdata":JSON.stringify({email:$scope.user.email, password:$scope.user.password})}

and in code igniter you can recive the data :

在代码点火器中,您可以重新获取数据:

$this->input->post("myformdata"); //should return what
                                  // that JSON.stringify returned

#5


1  

Although this is already answered I quite often have this quick little utility included in many of my applications that need to accept both application/x-www-form-urlencoded and application/json POSTs.

虽然这已经得到解答,但我经常在我的许多应用程序中包含这个快速的小实用程序,它需要同时接受application / x-www-form-urlencoded和application / json POST。

if (strcasecmp($_SERVER['REQUEST_METHOD'], 'post') === 0 && stripos($_SERVER['CONTENT_TYPE'], 'application/json') !== FALSE) {
    // POST is actually in json format, do an internal translation
    $_POST += json_decode(file_get_contents('php://input'), true);
}

After that point you can now just use the $_POST superglobal as you normally would, all the JSON data will be decoded for you.

在此之后,您现在可以像往常一样使用$ _POST超全局,所有JSON数据都将为您解码。