Javascript 我们能一次将表单数据全部传递给http请求吗

Javascript 我们能一次将表单数据全部传递给http请求吗,javascript,angularjs,Javascript,Angularjs,因此,我想知道,当在Angular中执行http请求时,是否可以同时传递所有表单数据 我的实际请求 $http({ method: 'post', url: 'ajax-processor.php', data: {'ajaxKey':'Mykey', 'angularQuery': 'login', 'username': $scope.username, 'password':$scope.password} }).then(func

因此,我想知道,当在Angular中执行http请求时,是否可以同时传递所有表单数据

我的实际请求

$http({
        method: 'post',
        url: 'ajax-processor.php',
        data:  {'ajaxKey':'Mykey', 'angularQuery': 'login', 'username': $scope.username, 'password':$scope.password}
    }).then(function successCallback(response) {
// dealing with response here
}
那么,有没有一种方法可以将所有表单字段传递给数据呢。避免提出各种不同的要求,只提出一个。

当然

如果您有可用的jQuery:

$('form').serialize();       // ---> "foo=1&bar=2"
$('form').serializeArray();  // ---> "[ { "foo": 1 }, { "bar": 2} ]"
第一个为您提供表单数据,第二个为您提供用于JSON的数组

如果jQuery不可用,而您正在使用HTML5:

var data = new FormData(document.querySelector('form'));
然后将其传递给ajax方法


如果您没有HTML5或jQuery,您可以自己编写代码来序列化表单,但这可能会很棘手,因此我建议使用某种库,例如
formserialize

实际上,您将如何在php端使用它?im使用$postdata=文件获取内容(“php://input");强烈建议使用AngularJS指令
ng model
,这样您就不必使用jQuery(如果可以的话,您真的不应该将jQuery与AngularJS混合使用)——这里有文档。使用
ng model
将表单输入绑定到范围变量,可以将表单数据作为一个序列化的JSON对象进行处理,您可以非常轻松地对其进行操作或传递到$http。这是我用angular替换jquery后想到的第一件事。