【问题标题】:how to post data From angular js to zend controller?如何将数据从 Angular js 发布到 zend 控制器?
【发布时间】:2013-08-19 06:47:23
【问题描述】:

查看页面:

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

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

【问题讨论】:

  • 要成为有效的 HTTP POST,您不需要在 JSON 提交中附加 HTML 名称吗?在登录控制器中,添加error_log( file_get_contents("php://input") );
  • 这个问题似乎是题外话,因为它是关于辅导

标签: php angularjs zend-framework2


【解决方案1】:

像这样改变你的 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 控制器。希望对您有所帮助。

【讨论】:

    【解决方案2】:

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

    AngularJs http post does not send data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多