【问题标题】:Codeigniter + Angular Js: How to receive JSON dataCodeigniter + Angular Js:如何接收 JSON 数据
【发布时间】:2014-07-02 19:15:31
【问题描述】:

情况是这样的:

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

应用程序中有一个登录系统。当用户输入邮箱和密码时,该数据被发送到服务器,如果邮箱存在且密码匹配,则返回true。

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

这是代码:

形式:

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

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

    });

}

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

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

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

所以问题是:

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

感谢您的帮助!

编辑:

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

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

但返回的是布尔值 false。

【问题讨论】:

  • 把它放在控制器print_r(json_decode(file_get_contents('php://input'))); 并粘贴结果请。
  • 是的,这就是关键!我发现半小时前。我正准备回答。如果你想做,我会做你的正确答案!
  • 很高兴帮助约翰尼 :)
  • 我认为这是最好的解决方案:stackoverflow.com/questions/11442632/…

标签: php json angularjs codeigniter post


【解决方案1】:

虽然这个问题已经得到解答,但我经常在需要接受 application/x-www-form-urlencodedapplication/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);
}

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

【讨论】:

    【解决方案2】:

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

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

    在代码点火器中,您可以接收数据:

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

    【讨论】:

      【解决方案3】:

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

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

      你可以测试一下

      print_r(json_decode(file_get_contents('php://input')));
      
      【解决方案4】:

      用途:

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

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

      我还建议你打开 XSS 过滤。

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

      【讨论】:

      • @johnnyfittizio 我对 AngularJS 一无所知,但您可以尝试将调用的数据属性更改为:data : {email:$scope.user.email, password:$scope.user.password}
      • 最简单的方法是传递用户对象。 data : $scope.user 在这种情况下,数据将作为 json 对象发送。但是我如何在 codeigniter 中接收它?
      • 通过使用输入类,Codeigniter 应该自动对您从帖子传递的数据执行 json_decode。您是否尝试在 ajax 调用中将 contentType 和/或 dataType 设置为 JSON?
      • 是的,已设置(并已编辑问题)。但如果我不发送从表单中获取的数据,而是发送假字符串数据。如何在 codeigniter 中接收到它。我认为这就是重点。
      • 顺便说一下 Angular 很棒!
      【解决方案5】:

      $_POST 在 CodeIgniter 中将为空,因为出于安全原因,它故意将其清空。您需要改用$this-&gt;input-&gt;post();

      CodeIgniter Input Class

      【讨论】:

        猜你喜欢
        • 2022-01-25
        • 2017-08-09
        • 2019-05-07
        • 1970-01-01
        • 1970-01-01
        • 2016-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多