![](/img/trans.png)
[英]I am getting null pointer exception when i get the data from xml document using web service
[英]I am not getting data on to my restful web service using angularjs
這是我的Angular js代碼,我想將Json格式的數據發送到用Java編寫的靜態Web服務。 它可以打電話給網絡服務,但我沒有任何數據。
<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" />
這是我需要數據的寧靜的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);
}
返回響應; }
您不應該使用@QueryParam
因為您不使用GET
方法,而是使用POST
方法。 使用POST
,您不會使用url傳輸查詢參數,而是傳輸包含在請求正文中的數據。
因此,從其余控制器方面,如果Data
類包含與發送的參數名稱和值匹配的字段和字段類型,則可以將您用作Data
類的參數用作信息。
@Path("/insert")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public String insert(Data data) {
...
}
你應該做這個:
$scope.users = data;
代替這個:
$scope.users = status;
狀態僅是請求的響應代碼。 希望這有所幫助)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.