我从Angular $ http电话发送userId。 servlet正在命中,但它无法接收请求参数。我通过request.getParameter获取请求参数(" userId");
这是代码
$scope.getDetailsFromServer = function() {
$http({
method: 'GET',
url: 'dashboardDetailsOfUser',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data:"userId="+ $scope.userId
}).success(function (data){
$scope.status=data;
}).error(function(data, status, headers, config) {
alert("error");
});
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
String result = "";
try {
con = ds.getConnection();
stmt = con.createStatement();
String user_id = request.getParameter("userId");
System.out.println("User ID :"+user_id);
String json = new Gson().toJson(result);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (stmt != null)
stmt.close();
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Angular或Servlet的代码中是否缺少任何内容?
答案 0 :(得分:0)
客户端发送GET请求,因此servlet使用请求主体not来填充请求参数。
发送POST并在servlet中使用doPost
,或者使用GET并在URL中包含userId参数。