【发布时间】:2016-02-27 19:06:57
【问题描述】:
我正在使用 html 中的表单,使用 angular,并绑定用户的用户名和电子邮件 ID。
我希望将特定用户的详细信息显示到服务器页面上,其中当用户单击前端的提交详细信息时,数据被“绑定”并且信息同时显示在服务器页面上。
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
<link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<title>Random title here</title>
<body ng-app="test" ng-controller="mainctrl as ctrl">
<div class="container">
<div class="col-sm-8 col-sm-offset-2">
<form name="userform" ng-submit="ctrl.submit()">
<div class="form-group">
<label> Name</label>
<input type="text" name="name" class="form-ctrl" ng-model="ctrl.user.name">
</div>
<div class="form-group">
<label> Email id</label>
<input type="email" name="email" class="form-ctrl" ng-model="user.email">
</div>
<button type="submit" class="btn btn-primary"> Submit </button>
</form>
</div>
</div>
<script>
var test=angular.module('test',[]);
test.controller('mainctrl',[function($scope,$html){
$scope.user={};
$scope.submit= function () {
$http({
method : 'POST',
url : 'test1.java', //No idea what to put here. Help pls.
data : $scope.user;
headers : {'Content-Type':'application/x-www-form-urlencoded'}
}).success(function(data)
{
console.log("Success");
});
}
}]);
</script>
</body>
</html>
这只是我键入的代码示例。作为初学者,任何帮助将不胜感激。谢谢。
【问题讨论】:
-
你好。据我了解,您想将数据发送到后端?
-
您需要将 URL 放在您的休息端点那里。尝试检查 chrome 控制台中的错误,看看 angular 是如何形成完整 URL 的。如果这不正确,请尝试更正它。
-
@BakhtierGaibulloev 是的。现在,我只希望当用户提交他的数据时,即他的用户名和电子邮件 ID,数据需要显示在服务器页面上。在经过几个环节之后,大家一般都是通过GET或者POST获取$http里面的数据,然后把信息传递给UI。但在这里,我想做相反的事情。只需从表单中读取数据,并将其显示在服务器页面上。
标签: javascript html angularjs http server