私は現在spring mvc
アプリケーションを開発中ですので、JSON array
を投稿する必要があります。jqueryでjavaサーブレットにjson配列を投稿するには
私はのparam attibuteをフェッチするためにrequest.getParameter("paramValue")
にアクセスし、それはここで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()も試しました –