[英]AngularJS- How to use $http.post to pass multiple parametres to a server class
I am new to AngularJS and web services. 我是AngularJS和Web服务的新手。 I am doing a program that gets value from the user->[name and age] and insert those values to the oracle database.
我正在做一个程序,该程序从user-> [name and age]中获取值,并将这些值插入oracle数据库。 I only was able to insert a single value.
我只能插入一个值。 My search for using $http.post to pass multiple values didn't turned out good.
我使用$ http.post传递多个值的搜索结果并不理想。 It would be so much helpful if somebody could help.
如果有人可以帮忙,那将有很大帮助。 Here's the code
这是代码
Client side code 客户端代码
<html>
<title>My AngularJS App</title>
<body ng-app="myApp" ng-controller="DBCtrl">
<script type ="text/javascript" src="https://ajax.googleapis.co/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<form ng-submit="insertData()">
Student name: <input type = "text" ng-model="name" >
Student age: <input type= "text" ng-model="age">
<br>
<input type ="submit" value="INSERT">
</form>
<p>{{msg}}</p>
<script>
var app = angular.module('myApp',[]);
app.controller('DBCtrl', function($scope,$http){
$scope.insertData = function(){
alert($scope.name);
$http.post('rest/DB/add',$scope.name)
//$http.get("rest/DB/extract")
.success(function(){
$scope.msg="DATA INSERTED";
})
}
});
</script>
</body>
</html>
Server Side Java Code 服务器端Java代码
package com.ustri.DBman;
@Path("/DB")
public class DBManager {
@POST
@Path("/add")
@Produces(MediaType.TEXT_HTML)
public void addDetails(String sname,String sage){
System.out.println("IN add");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hr","hr");
//step3 create the statement object
System.out.println("Connection established successfully");
PreparedStatement stmt=con.prepareStatement("insert into studreg values(?,?)");
System.out.println(sname+"+"+sage);
stmt.setString(1,sname);
stmt.setString(2,sage);
int i=stmt.executeUpdate();
System.out.println(i+" records inserted");
con.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Using this code I am only able to insert a single value $scope.name. 使用此代码,我只能插入单个值$ scope.name。 How can I modify my code to pass both $scope.name and $scope.age parametres through $http.post to Server?
如何修改我的代码,以将$ scope.name和$ scope.age参数通过$ http.post传递到服务器?
Using this code I am only able to insert a single value $scope.name.
使用此代码,我只能插入单个值$ scope.name。 How can I modify my code to pass both $scope.name and $scope.age parametres through $http.post to Server?
如何修改我的代码,以将$ scope.name和$ scope.age参数通过$ http.post传递到服务器?
By default, the $http
post/get methods transform the requests by serializing the data as JSON and post them with the "application/json"
content-type. 默认情况下,
$http
post / get方法通过将数据序列化为JSON来转换请求,并使用"application/json"
内容类型将其发布。
While you seem to want to post your data with the "application/x-www-form-urlencoded"
content-type. 尽管您似乎想使用
"application/x-www-form-urlencoded"
内容类型发布数据。
You could specify the "application/x-www-form-urlencoded"
content type and create the data with the good format (adding the &
separator between the transmit values) before sending them. 您可以指定
"application/x-www-form-urlencoded"
内容类型,并在发送它们之前使用格式正确的数据(在传输值之间添加&
分隔符)。
Or else you could do simpler. 否则您可以做得更简单。
You can send a JS object that contains both information. 您可以发送包含两个信息的JS对象。
replace 更换
$http.post('rest/DB/add',$scope.name);
by 通过
var postedObj = {'name':$scope.name, 'age':$scope.age}
$http.post('rest/DB/add',postedObj);
and change your rest controller according to. 并根据更改您的休息控制器。
Replace 更换
public void addDetails(String sname,String sage){
by 通过
public void addDetails(Details details){
where Details
has as fields the two transmitted values. 其中“
Details
具有两个传输值作为字段。
public class Details{
private String name;
private String age;
// and add getters and setters if required by your JSON/Java mapper.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.