Java AngularJS http POST 到 Servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31401581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS http POST to Servlet
提问by v1shnu
I went through a lot of StackOverflow answers and googled a lot but still could not find why my post request is not working.
我浏览了很多 StackOverflow 的答案并用谷歌搜索了很多,但仍然找不到我的帖子请求不起作用的原因。
This is my jsp:
这是我的jsp:
<div class="container">
<form class="form-signin" ng-controller="MyController">
<h2 class="form-signin-heading">Please sign in</h2>
<label for="username" class="sr-only">Username</label>
<input type="text" id="username" ng-model="user.name" class="form-control" placeholder="Username" required autofocus>
<label for="password" class="sr-only">Password</label>
<input type="password" ng-model="user.password" id="password" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" ng-click="login()" type="submit">Sign in</button>
</form>
</div>
This is my controller:
这是我的控制器:
app.controller('MyController', function($scope, $http) {
$scope.login = function() {
console.log($scope.user);
$http({
method : 'POST',
url : 'login',
data : $scope.user,
headers: {
'Content-Type': 'application/json'
}
}).success(function(data) {
console.log(data);
}).error(function(data) {
console.log(data);
});
console.log("POST done");
};
});
And my servlet:
还有我的 servlet:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("inside do POST");
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonObject obj = (JsonObject) parser
.parse(request.getParameter("data"));
Iterator it = (Iterator) obj.entrySet();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("over");
}
I keep getting this Null pointer exception
我不断收到这个空指针异常
java.lang.NullPointerException
at java.io.StringReader.<init>(StringReader.java:50)
at com.google.gson.JsonParser.parse(JsonParser.java:45)
at com.zookeeperUI.controller.Login.doPost(Login.java:40)
Please tell me what am I doing wrong here .
请告诉我我在这里做错了什么。
采纳答案by wero
Your request does not contain a URL parameter named "data", therefore request.getParameter("data") returns null and you get the NullPointerException.
您的请求不包含名为“data”的 URL 参数,因此 request.getParameter("data") 返回 null 并且您得到 NullPointerException。
You try to send a Javascript object via URL parameters which does not go well with non-shallow objects.
您尝试通过 URL 参数发送 Javascript 对象,这不适用于非浅层对象。
I would recommend to send the data as request payload:
我建议将数据作为请求有效负载发送:
JsonObject obj = (JsonObject) parser.parse(request.getReader());
On the client you need to make sure that your data is sent as proper JSON:
在客户端,您需要确保您的数据以正确的 JSON 格式发送:
$http({
method : 'POST',
url : 'login',
contentType: 'application/json',
data : JSON.stringify($scope.user),
})...
回答by atinder
i think you should be sending data as
我认为你应该发送数据
$http({
method : 'POST',
url : 'login',
data : {data: $scope.user},
headers: {
'Content-Type': 'application/json'
}
}).success(function(data) {
console.log(data);
});
pay attention to data : {data: $scope.user}
注意 data : {data: $scope.user}
回答by Michael
You are using a POST
request and your data is sent in the request body- not as parameter. You need to read the content using request.getReader()
.
您正在使用POST
请求,并且您的数据在请求正文中发送,而不是作为参数发送。您需要使用 阅读内容request.getReader()
。
// example using the javax.json.stream package
JsonParser parser = Json.createParserFactory().createParser(request.getReader());