0
<form ng-submit="doRegister()">
    <div class="control-group">
        <label for="email">E-Mail Address</label>
        <input type="email" id="email" name="email" ng-model="user.email" autofocus>
    </div>
    <div class="control-group">
        <label for="password">Password</label>
        <input type="password" id="password" name="user.password" ng-model="password">
    </div>
    <div class="control-group">
        <label for="password_confirmation">Confirm Password</label>
        <input type="password" id="password_confirmation" name="password_confirmation" ng-model="user.password_confirmation">
    </div>
    <input type="submit">
</form>

如何将表单提交给registervia POST?从技术上讲,我不知道data可以放置在哪里。

function registerCtrl ($scope, $http) {
    document.title = 'Register';
    $scope.doRegister = function(){
        $http.post('/register', data).
            success(function(data, status, headers, config) {
                console.log(data);
            }).
            error(function(data, status, headers, config) {
                angular.element('.errors').html(data.errors.join('<br>')).slideDown();
            });
    };
}

编辑:进展:

function registerCtrl ($scope, $http) {
    document.title = 'Register';
    $scope.user = {};
    $scope.doRegister = function(){
        $http.post('/register', $scope.user);
    };
}

但是发送到服务器的请求在中是一个空数组:

Array()
4

4 回答 4

1

您需要声明变量并进行headers如下更改:

function registerCtrl ($scope, $http) {
    document.title = 'Register';
    $scope.user = {
        email: '',
        password: ''
    };
    $scope.doRegister = function(){
        $http({
            url: '/register', 
            data: $scope.user,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj)
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                return str.join("&");
            },
        });
    };
}

你需要

于 2013-06-28T12:35:59.080 回答
1

您已经在表单中绑定了一个属性。您可以使用它们定义数据。

例如,对于电子邮件字段:

var data = {};
data.email = $scope.email;

或者您甚至可以$scope.data = {}在控制器中定义并发布它。


编辑于 9/14

这似乎是人们使用 $http.post() 发送表单数据时遇到的问题之一。解决方案在以下问答中给出。

AngularJS - $http.post 发送请求参数而不是 JSON 的任何方式?

于 2012-09-13T09:07:22.380 回答
0

AngularJS 的 post 方法将数据作为 Request Payload 发送。

只需使用:

$json = json_decode(file_get_contents("php://input"));

您可以通过使用访问 php 中的值

$email = $json->email;
$password = $json->password;
$password_confirmation = $json->password_confirmation;
于 2014-01-15T16:06:06.600 回答
-2
<form id="Register" action="/register">
 <div class="control-group">
        <label for="email">E-Mail Address</label>
        <input type="email" id="email" name="email" ng-model="email" autofocus>
    </div>
    <div class="control-group">
        <label for="password">Password</label>
        <input type="password" id="password" name="password" ng-model="password">
    </div>
    <div class="control-group">
        <label for="password_confirmation">Confirm Password</label>
        <input type="password" id="password_confirmation" name="password_confirmation" ng-model="password_confirmation">
    </div>
    <input type="submit">
</form>

调用ajax函数。

$.ajax({
    url: form.prop('action'),
    type: 'POST',
    data: form.serialize(),
    dataType: 'json',
    async: false,
    success: function (result) {

    }
}); // ajax post end

var form = $("#Register");form.serialize()很容易从服务器端传递数据的数据传递 。

于 2012-09-13T08:59:38.673 回答