【发布时间】:2015-11-20 06:56:05
【问题描述】:
感谢发布 http 请求,我尝试从 angularJS 客户端发送 JSON 格式的数据,并感谢 j2ee servlet。但我遇到了一个错误。由于我的 servlet 中的 getParameterNames 方法可以访问我的完整数据,但我无法通过其他方式获取它。
我不明白为什么我的数据是键而不是值。
AngularJS 客户端
setParametersForTools : function (toolName, data) {
var jsonData = JSON.stringify(data) // I try with json and stringify json
var promise =
$http({
url: configuration.root_url_api+"SetParametersServlet?tool="+toolName,
method: "POST",
dataType: 'json',
data: data,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
.then(function (response){
console.log(response);
}, function (error){
console.log(error);
})
return promise;
}
Servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String toolname = request.getParameter("tool"); //toolname is correct
String json = request.getParameter("data"); // return null...
Enumeration<String> paramsName = request.getParameterNames();
for (;paramsName.hasMoreElements();) {
String paramName=paramsName.nextElement();
System.out.println("param:"+paramName);
}
}
Servlet 日志
//For Parameter names
param:tool
param:{ my correct data in json format}
也许我没有正确发送数据,但经过多次搜索后我不明白出了什么问题。
【问题讨论】:
-
更改数据:数据,数据:jsonData,在您的 ajax 中
标签: json angularjs jakarta-ee servlets post