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>

registerにフォームを送信するにはどうすればよい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

フォーム内にバインドされたプロパティが既にあります。それらを使用してデータを定義できます。

たとえば、電子メール フィールドの場合:

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

または、コントローラーで定義$scope.data = {}して投稿することもできます。


9/14に編集

これは、フォーム データを送信するための $http.post() で見られる問題の 1 つと思われます。解決策は、次の Q&A に記載されています。

AngularJS - $http.post が JSON の代わりにリクエスト パラメータを送信する方法はありますか?

于 2012-09-13T09:07:22.380 に答える
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 に答える
0

AngularJS の post メソッドは、データをリクエスト ペイロードとして送信します。

使用するだけです:

$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 に答える