3

查看页面:

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

登录控制器:

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

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

4

2 回答 2

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 控制器。希望能帮助到你。

于 2013-08-19T07:00:39.677 回答
0

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

AngularJs http post不发送数据

于 2015-05-11T22:08:03.027 回答