【发布时间】: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