将表单数据提交到服务器端
submit form data to serverside
<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>
如何通过POST
将表格提交给register
?从技术上讲,我不知道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);
};
}
但是发送到服务器的请求在 PHP 中是一个空数组:
Array()
窗体中已绑定了属性。您可以使用它们定义数据。
例如,对于电子邮件字段:
var data = {};
data.email = $scope.email;
或者您甚至可以在控制器中定义$scope.data = {}
,并发布它。
编辑 9/14
这似乎是人们看到的发送$ http.post((的问题之一表单数据。 解决方案在下面的问答中给出。
AngularJS - 有什么方法可以花 http.post 美元来发送请求参数而不是 JSON?
您需要
声明变量并更改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("&");
},
});
};
}
你需要
AngularJS的post方法将数据作为请求有效负载发送。
只需使用:
$json = json_decode(file_get_contents("php://input"));
您可以使用 php 访问值
$email = $json->email;
$password = $json->password;
$password_confirmation = $json->password_confirmation;
<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()
非常容易从服务器端传递数据。
相关文章:
- 如何使用skip参数使用angular ui引导进行服务器端分页
- Webpack开发服务器和React服务器端渲染
- 提示使用服务器端事件处理程序激活JavaScript
- 使用谷歌应用程序脚本将服务器端数据表返回到客户端
- 如何轻松地将服务器端变量从Java代码转移到客户端代码
- 将表单数据提交到服务器端
- 如何向服务器端提交javascript变量
- 在没有服务器端脚本的情况下提交表单
- 在表单提交时将客户端 HTML 表数据提取到服务器端 PHP 脚本
- 在将数据发送到服务器端(php)之前,提交按钮出错
- 如何保存提交的HTML表单的服务器端副本
- 如何使用提交按钮(onclick)在javaScript(服务器端)上创建消息框
- 用javascript将输入文件提交到服务器端
- 如何在提交前执行服务器端验证
- 用javascript在asp.net中取消服务器端提交功能
- 当我需要运行服务器端代码时,我如何响应按钮的单击(不提交页面)?
- 发布数据服务器端并在提交表单时执行javascript代码
- 在服务器端模型验证通过后,禁用mvc中的提交按钮
- JavaScript-在提交到服务器端之前,如果所有字段都已填写,则可以在表单中进行检查
- 当使用plupload HOWTO时,选择复制文件对象&单独提交以进行相关的服务器端处理