[英]I am not getting data on to my restful web service using angularjs
This is my angular js code in which i want to send data in Json format to my restful web service written in java. 这是我的Angular js代码,我想将Json格式的数据发送到用Java编写的静态Web服务。 It makes call to the webservice but i don't get any data.
它可以打电话给网络服务,但我没有任何数据。
<script>
var app = angular.module("myPost", []);
app.controller("myControl", function ($scope, $http) {
$scope.title = "",
$scope.rating = "",
$scope.feedback = "",
$scope.role = "";
$scope.send = function (title, rating, feedback, role) {
var feed = {
title: title,
rating: rating,
feedback: feedback,
role: role
};
var head = {'Content-Type': 'application/json'};
$http({
method: 'POST',
url: 'myurl',
data: feed,
headers: head
}).success(function (data, status, headers, config) {
$scope.users = data;
}).error(function (data, status, headers, config) {
$scope.error = status;
});
};
});
</script>
</head>
<body ng-app="myPost" ng-controller="myControl">
<form action="">
Title: <input type="text" name="title" value="" ng-model="title" />{{title}}<br>
Rating:<br> <select name="rating" ng-model="rating">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select><br>
Feedback: <input type="text" name="feedback" value="" ng-model="feedback"/><br>
<input type="hidden" name="type" value="article" ng-model="role" />
<input type="submit" ng-click="send(title, rating, feedback, role)" value="submit" />
This is my restful Web Service where I want the data. 这是我需要数据的宁静的Web服务。
@Path("/database")
public class database {
@Path("/insert")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String insert(@QueryParam("title") String title,
@QueryParam("rating") int rating,
@QueryParam("feedback") String feedback,
@QueryParam("role") String role) {
Data d = new Data();
d.setTitle(title);
d.setRating(rating);
String response = new Gson.toJson(d);
}
return response; 返回响应; }
}
You should not use @QueryParam
since you don't use the GET
method but the POST
method. 您不应该使用
@QueryParam
因为您不使用GET
方法,而是使用POST
方法。 With POST
, you don't transmit query params with the url but you transmit data enclosed in the body of the request. 使用
POST
,您不会使用url传输查询参数,而是传输包含在请求正文中的数据。
So from the rest controller side, to retrieve the information you can use as parameter your Data
class if it contains the fields and types of fields matching with sent parameter names and values. 因此,从其余控制器方面,如果
Data
类包含与发送的参数名称和值匹配的字段和字段类型,则可以将您用作Data
类的参数用作信息。
@Path("/insert")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String insert(Data data) {
...
}
You should do this: 你应该做这个:
$scope.users = data;
instead of this: 代替这个:
$scope.users = status;
The status is only the response code of the request. 状态仅是请求的响应代码。 Hope this helped)
希望这有所帮助)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.