我目前正在開發spring mvc
應用程序,我需要張貼JSON array
。如何發佈一個JSON數組的Java Servlet與jQuery
當我訪問request.getParameter("paramValue")
獲取帕拉姆attibute,但它返回一個null
值,
這裏是我的前端代碼:
$.ajax(url, {
async: true,
type: 'post',
contentType: 'application/json',
data: JSON.stringify({
"test":"test value"
})
}).done(function (response) {
console.log(data);
}).fail(function (xhr) {
console.log("request failed");
console.log(xhr);
});
這裏是我的服務器端代碼:
@RequestMapping(value = "/Products", method = RequestMethod.POST)
public void saveProducts(HttpServletRequest req, HttpServletResponse res) throws Exception {
System.out.println(req.getContentType());
System.out.println(req.getContentLength());
System.out.println(req.getContextPath());
System.out.println(req.getParameterValues("test"));
System.out.println(req.getMethod());
StringBuilder buffer = new StringBuilder();
BufferedReader reader = req.getReader();
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String data = buffer.toString();
System.out.println(data);
System.out.println(req.getParameter("test"));
}
的輸出是:
application/json
22
null
POST
{"test" : "Test DAta"}
null
我無法弄清楚怎麼回事,請幫幫我。
您發送的對象,而不是參數。可能重複 – nllsdfx
沒有我試圖JSON.stringify()也 –