- form方式
$.ajax({
url: 'saveP',
data: {
registerId: registerId,
cause: cause,
drugs: drugIds
},
dataType: 'json',
contentType: 'application/x-www-form-urlencoded',
type: 'post',
success: function (result) {}
});
- 方式一:使用 @RequestParam(value = "drugs[]"),注意名字有中括号[]
public Result saveP(String registerId, String cause, @RequestParam(value = "drugs[]")String[] drugs) {
}
- 方式二:使用HttpServletRequest.getParameterValues
public Result saveP(HttpServletRequest request) {
String registerId = request.getParameter("registerId");
String cause = request.getParameter("cause");
String[] drugs = request.getParameterValues("drugs[]");
}
- json方式
- 前端
要注意processData参数为false
$.ajax({
url: 'saveP',
data: {
registerId: registerId,
cause: cause,
drugs: drugIds
},
dataType: 'json',
processData: false,
contentType: 'application/json',
type: 'post',
success: function (result) {}
});
- 后端,将HttpServletRequest流转成JSONObject
public Result saveP(HttpServletRequest request) {
String param= null;
try {
BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
JSONObject jsonObject = JSONObject.parseObject(responseStrBuilder.toString());
param= jsonObject.toJSONString();
System.out.println(param);
} catch (Exception e) {
e.printStackTrace();
}
}
其他
https://blog.csdn.net/achenyuan/article/details/81114517