无法在 servlet 中发送参数



我有角度代码

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

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

您正在将data作为 POST 请求正文发送,而不是请求参数。这记录在 post 方法的角度文档中:

url 字符串
指定请求目标的相对或绝对 URL

数据 *
请求内容

看看这个答案,看看如何阅读请求正文。

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

最新更新