AngularJS http POST 到 Servlet [英] AngularJS http POST to Servlet
问题描述
我浏览了很多 StackOverflow 的答案并在谷歌上搜索了很多,但仍然找不到我的帖子请求不起作用的原因.
这是我的jsp:
<form class="form-signin" ng-controller="MyController"><h2 class="form-signin-heading">请登录</h2><label for="username" class="sr-only">用户名</label><input type="text" id="username" ng-model="user.name" class="form-control" placeholder="Username" required autofocus><label for="password" class="sr-only">密码</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">登录</button></表单>
这是我的控制器:
app.controller('MyController', function($scope, $http) {$scope.login = function() {console.log($scope.user);$http({方法:'POST',网址:'登录',数据:$scope.user,标题:{'内容类型':'应用程序/json'}}).成功(功能(数据){控制台日志(数据);}).错误(函数(数据){控制台日志(数据);});console.log("POST 完成");};});
还有我的 servlet:
protected void doPost(HttpServletRequest request,HttpServletResponse 响应) 抛出 ServletException, IOException {System.out.println("里面做POST");Gson gson = new Gson();JsonParser 解析器 = new JsonParser();JsonObject obj = (JsonObject) 解析器.parse(request.getParameter("data"));Iterator it = (Iterator) obj.entrySet();而 (it.hasNext()) {System.out.println(it.next());}System.out.println("结束");}
我不断收到空指针异常
java.lang.NullPointerException在 java.io.StringReader.(StringReader.java:50)在 com.google.gson.JsonParser.parse(JsonParser.java:45)在 com.zookeeperUI.controller.Login.doPost(Login.java:40)
请告诉我我在这里做错了什么.
您的请求不包含名为data"的 URL 参数,因此 request.getParameter("data") 返回 null 并且您得到 NullPointerException.
您尝试通过 URL 参数发送 Javascript 对象,这不适用于非浅层对象.
我建议将数据作为请求负载发送:
JsonObject obj = (JsonObject) parser.parse(request.getReader());
在客户端,您需要确保您的数据以正确的 JSON 格式发送:
$http({方法:'POST',网址:'登录',内容类型:'应用程序/json',数据:JSON.stringify($scope.user),})...
I went through a lot of StackOverflow answers and googled a lot but still could not find why my post request is not working.
This is my 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:
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 .
Your request does not contain a URL parameter named "data", therefore request.getParameter("data") returns null and you get the NullPointerException.
You try to send a Javascript object via URL parameters which does not go well with non-shallow objects.
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:
$http({
method : 'POST',
url : 'login',
contentType: 'application/json',
data : JSON.stringify($scope.user),
})...
这篇关于AngularJS http POST 到 Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!