php - 如何将数据从 angular js 发布到 zend Controller ?

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

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

登录 Controller :

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

我需要从 Angular 形式获取数据并将其传递给 zend Controller 并执行登录检查和提交表单。任何人都可以帮助逐步解释吗?

最佳答案

像这样更改您的 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 Controller 。希望对您有所帮助。

关于php - 如何将数据从 angular js 发布到 zend Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18307876/

相关文章:

javascript - 在 Angular 中加载配置文件,然后再进行其他操作

php - Zend 2-理论如何为三个实体一对多插入?

postgresql - 在 ZF2 中使用 tablegateway 连接 2 个表

php - 我可以在代码上强制执行哪些类型的模式以使其更容易转换为另一种编程语言?

c# - 将 C# 字符串发送到 .PHP 页面

php - 使用 jQuery-AJAX 向 PHP 提交表单并在 div 中显示新内容而不刷新的问题

php - 为什么我们需要构造函数?

php - 如何使用 easyxdm 跨域自动调整 WordPress 页面内 iframe 的大小?

javascript - 页面重定向在功能中不起作用

javascript - AngularJS Web应用程序查看页面源仅显示 "index.html"内容