AngularJS http POST到Servlet [英] AngularJS http POST to Servlet

查看:98
本文介绍了AngularJS http POST到Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了很多StackOverflow的答案并且搜索了很多但仍然无法找到我的帖子请求无效的原因。

I went through a lot of StackOverflow answers and googled a lot but still could not find why my post request is not working.

这是我的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> 

这是我的控制器:

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");
    };
});

我的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");
    }

我一直得到这个Null指针异常

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 .

推荐答案

您的要求不包含名为data的URL参数,因此request.getParameter(data)返回null并且您获得NullPointerException。

Your request does not contain a URL parameter named "data", therefore request.getParameter("data") returns null and you get the NullPointerException.

您尝试发送Javascript对象通过URL参数,这些参数与非浅层对象不相符。

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());

在客户端,您需要确保将数据作为正确的JSON发送:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆