angularJs$http。无法从请求中修改参数
angularjs代码:angularJs$http。无法从请求中修改参数,angularjs,Angularjs,angularjs代码: $http({ method: 'POST', url:'/hello/rest/user/test', data: {user: 'aaaaa'} }); 服务器代码: @POST @Path("/test") public Response merge(@Context HttpServletRequest request) { System.out.println(request.getParameter("user"));
$http({
method: 'POST',
url:'/hello/rest/user/test',
data: {user: 'aaaaa'}
});
服务器代码:
@POST
@Path("/test")
public Response merge(@Context HttpServletRequest request) {
System.out.println(request.getParameter("user"));
if (request.getParameter("user") == null) {
return Response.status(Status.BAD_REQUEST).build();
}
return Response.ok().build();
}
getParameter(“用户”)为空。我无法通过这种方式修改参数。您需要查看主体,而不是post参数 您可能没有收到POST参数
您正在接收一个类似于{“user”:“aaaaa”}的主体,您需要解析该JSON响应。如果您想使用请求参数,您需要将数据作为
$http.post('/hello/rest/user/test', {
user : 'aaaaa'
}, {
headers : {
"Content-Type" : 'application/x-www-form-urlencoded;charset=utf-8'
},
transformRequest : [function(data) {
return angular.isObject(data)
? jQuery.param(data)
: data;
}]
});
另请阅读application/x-www-form-urlencoded,表单。但我只想使用“application/json”。我不想使用“form”。如果您想将数据作为请求体ie json发送,则不能使用
request.getParameter()
。您需要使用@RequestBody
注释以及jackson库来访问反序列化的请求