如何发布数据从角度js到zend控制器?

时间:2013-08-19 06:47:24

标签: php angularjs zend-framework2

查看页面:

<div ng-controller="LoginCtrl">
<form name="login_form" ng-submit="submit()" >
    Email: <input type="email" ng-model="login.email" required/><br />
    Password: <input type="password" ng-model="login.pass" required/><br />

    <input type="submit" />
</form>

main.js

function LoginCtrl($scope, $http) {
$scope.login = {};
$scope.submit = function(){

    $http({
        method: 'POST',
        url: '/login',
        data: $scope.login,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}

    }).
        success(function(response) {
            window.location.href = "/login";
        }).
        error(function(response) {
            $scope.codeStatus = response || "Request failed";
        });
}

的LoginController:

if ($request->isPost()) {
            $data = json_decode(file_get_contents("php://input"));}

我需要从角形式获取数据并将其传递给zend控制器并执行登录检查并提交表单。有人可以帮助逐步解释吗?

2 个答案:

答案 0 :(得分:4)

像这样更改你的main.js;

function LoginCtrl($scope, $http) {
$scope.login = {email: "", password: ""};
$scope.login = $.param($scope.login);
$scope.submit = function(){

    $http({
        method: 'POST',
        url: '/login',
        data: $scope.login,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}

    }).
        success(function(response) {
            window.location.href = "/login";
        }).
        error(function(response) {
            $scope.codeStatus = response || "Request failed";
        });
}

在您的php文件中访问您的电子邮件和密码,如;

$email = $_REQUEST['email'];
$password = $_REQUEST['password'];

并将这些凭据发送到您的zend控制器。希望它有所帮助。

答案 1 :(得分:0)

我发现另一篇文章谈到角度不以php期望的方式发送数据。即它正在进行json编码。当我遇到这个问题时,REQUEST是空的。看看以下帖子:

AngularJs http post does not send data