【问题标题】:How do I send form data to server using angular?如何使用 Angular 将表单数据发送到服务器?
【发布时间】:2014-01-01 03:32:58
【问题描述】:

目前,我正在使用服务器端框架 CakePHP 来构建我的应用程序。

我需要将 Angular 集成到我的应用中。

目前,我发送以下输入

User.old_passwordUser.new_passwordUser.new_password_confirm

使用 POST 发送到此网址 /mypasswords/new

这很好用。

我知道要将数据发送到服务器端,我需要使用 Angular 中的服务。

到目前为止,这是我的 Angular 代码。

var app = angular.module("myApp", []);

app.controller('passwordController', ['$scope', function($scope) {
  $scope.submitted = false;
  $scope.changePasswordForm = function() {
    if ($scope.change_password_form.$valid) {
      // Submit as normal
    } else {
      $scope.change_password_form.submitted = true;
    }
  }
}]);

services = angular.module('myApp.services', ['ngResource']);

services.factory("UserService", function($http, $q) {
  var service;
  // service code here
});

那么我在//service code here 部分写什么?我还缺少哪些其他部分?

呈现后,我的表单目前看起来像 html。 (我知道我可能必须从表单中删除操作和方法属性。是吗?)

<form action="/mypasswords/new" id="change_password_form" name="change_password_form" ng-controller="passwordController" ng-submit="changePasswordForm()" method="post" accept-charset="utf-8" class="ng-scope ng-pristine ng-valid">

<div style="display:none;"><input type="hidden" name="_method" value="POST"></div>            

<input name="data[User][old_password]" id="u56" placeholder="Enter old password..." class="u56 profile_input" type="password">
<input name="data[User][new_password]" id="u57" placeholder="Enter new password..." class="u57 profile_input" type="password">
<input name="data[User][new_password_confirm]" id="u58" placeholder="Enter new password..." class="u58 profile_input" type="password">
<div id="u25" class="u25_container">
    <div id="u25_img" class="clear_all_button detectCanvas" onclick="document.getElementById('change_password_form').reset()">
     </div>
</div>

<div id="u27" class="u27_container">
    <input id="u27_img" class="submit_button detectCanvas" type="submit" value="SAVE">                         

</div>
</form>

谢谢。

【问题讨论】:

    标签: javascript angularjs cakephp


    【解决方案1】:

    如果你只想用ajax发送表单,不需要创建工厂,只需在控制器中这样做:

    var app = angular.module("myApp", [$http]);
    
    app.controller('passwordController', ['$scope', function($scope) {
      $scope.submitted = false;
      $scope.changePasswordForm = function() {
        if ($scope.change_password_form.$valid) {
          //note: use full url, not partial....
          $http.post('http://myweb.com/mypasswords/new', change_password_form.Data) 
          .success(function(data, status, headers, config) {
            //do anything when it success..
          })
          .error(function(data, status, headers, config){
            //do anything when errors...
          });
        } else {
          $scope.change_password_form.submitted = true;
        }
      }
    }]);
    

    您的表单将如下所示:

    <form id="change_password_form" name="change_password_form" ng-controller="passwordController" ng-submit="changePasswordForm()" method="post" accept-charset="utf-8" class="ng-scope ng-pristine ng-valid">
    
    <div style="display:none;"><input type="hidden" name="_method" value="POST"></div>            
    
    <input name="data[User][old_password]" ng-model="Data.old_password" id="u56" placeholder="Enter old password..." class="u56 profile_input" type="password">
    <input name="data[User][new_password]" ng-model="Data.new_password" id="u57" placeholder="Enter new password..." class="u57 profile_input" type="password">
    <input name="data[User][new_password_confirm]" ng-model="Data.new_password_confirm" id="u58" placeholder="Enter new password..." class="u58 profile_input" type="password">
    <div id="u25" class="u25_container">
        <div id="u25_img" class="clear_all_button detectCanvas" onclick="document.getElementById('change_password_form').reset()">
         </div>
    </div>
    
    <div id="u27" class="u27_container">
        <input id="u27_img" class="submit_button detectCanvas" type="submit" value="SAVE">                         
    
    </div>
    </form>
    

    【讨论】:

    【解决方案2】:

    第1步:使用普通html而不是CakePHP中的FormHelper编写表单

    表格应如下所示:

    <form name="change_password_form" ng-controller="passwordController"
             ng-submit="changePasswordForm()">
    
            <input name="data[User][old_password]" ng-model="data.User.old_password" type="password" placeholder="Enter old password..." class="u56 profile_input">
            <input name="data[User][new_password]" ng-model="data.User.new_password" type="password" placeholder="Enter new password..." class="u57 profile_input">
            <input name="data[User][new_password_confirm]" ng-model="data.User.new_password_confirm" type="password" placeholder="Enter new password again..." class="u58 profile_input">
    
                <div id="u25"
                     class="u25_container">
                    <div id="u25_img"
                         class="clear_all_button detectCanvas" onclick="document.getElementById('change_password_form').reset()">
                    </div>
                </div>
    
                <div id="u27"
                     class="u27_container">
                    <button type="submit" id="u27_img" ng-click="debug()"
                         class="submit_button detectCanvas" />
                </div>
            </form>
    

    第二步:按如下方式编写app.js。

    如果你想使用 CakePHP $this-&gt;request-&gt;is('ajax'),X-Requested-With 标头很重要

    angular.module("myApp", [])
    .controller('passwordController', function($scope, $http) {
      $scope.changePasswordForm = function() {
        console.log('change passwrd form activated');
        //note: use full url, not partial....
        $http({
            method  : 'POST',
            url     : '/mypasswords/new',
            data    : $.param($scope.data),  // pass in data as strings
            headers : { 'Content-Type': 'application/x-www-form-urlencoded',
            'X-Requested-With' : 'XMLHttpRequest'
            }  // set the headers so angular passing info as form data (not request payload)
        })
        .success(function(data, status, headers, config) {
            //do anything when it success..
            console.log('works!');
          })
        .error(function(data, status, headers, config){
            //do anything when errors...
            console.log('errors!');
        });
      }
      $scope.debug = function () {
       console.log($scope);
      }
    });
    

    【讨论】:

    • 谢谢!真的很有帮助
    猜你喜欢
    • 2018-07-02
    • 2019-06-27
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2023-03-26
    相关资源
    最近更新 更多