【问题标题】:Unable to send params in servlet无法在 servlet 中发送参数
【发布时间】:2018-08-25 10:56:11
【问题描述】:

我有角码

app.controller('add', function ($scope, $http) {
    $scope.add = function() {
        $scope.msg = "no connect";

        var data = {name:'soso',description:'buba',method:'POST'};

        $http.post(host + "/add-component",data)
            .then(function (response) {
                $scope.msg = response.data;
            });

    }
});

在我的 servlet 中,我想捕获它

resp.setContentType("application/json; charset = utf8");
String name = req.getParameter("name");
String description = req.getParameter("description");

但我的名字和描述都 = null;

【问题讨论】:

    标签: java angularjs http servlets


    【解决方案1】:

    您正在发送 data 作为 POST 请求正文,而不是请求参数。这记录在the angular docs for post method:

    网址字符串
    指定请求目的地的相对或绝对 URL

    数据 *
    请求内容

    看看at this answer to see how to read request body

    如今,使用 Spring Boot 或其他框架来处理服务器端端点变得更加容易。使用 Servlet 并没有错,但您必须自己编写更多代码。

    【讨论】:

    • 非常感谢!这对我真的很有帮助。 Map<string string> 参数 = new HashMap(); String[] body = req.getReader().lines().collect(Collectors.joining()).split("\""); for (int i = 1; i </string>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 2018-06-08
    • 2019-04-24
    • 2020-10-18
    相关资源
    最近更新 更多