第一种
- 前端将参数转为JSON类型
前端代码:
$.ajax({
type: "post",
url: baseUrl + "/stock/detail",//对应controller的URL
async: true,
dataType: 'json',
contentType : "application/json",
data: JSON.stringify(ids),//json对象转化为json字符串
success: function (ret) {
console.log(ret);
}
后端代码:
@RequestMapping(value = "/detail")
@ResponseBody
public String detail(HttpServletRequest req,@RequestBody String[] ids) {
if (!StringUtils.isEmpty(ids))
for (int i = 0; i < ids.length; i++) {
String idsss = ids[i];
System.out.println("requestBody获取传递的json数组参数:" + id);
}
}else{
System.out.println("requestBody获取传递的json数组参数为空");
}
第二种
- 前端js 传递json字符串,设置contentType:"application/json"
前端代码:
var dataObj = {
"ids":ids
}
var dataJsonStr = JSON.stringify(dataObj);
$.ajax({
type:"post",
url:baseUrl+"/stock/detail",//对应controller的URL
async:true,
contentType : "application/json",
dataType: 'json',
data: dataJsonStr,
success:function(ret){}
后端代码:
@RequestMapping(value = "/detail",method = RequestMethod.POST)
@ResponseBody
public Map<String,Object> detail(@RequestBody TStockInventoryDetail detail){//
第三种
- 前端参数不做任何处理
前端代码:
var ids = new Array();
ids.push(1);
$.ajax({
type: "post",
url: baseUrl + "/stock/detail.json",//对应controller的URL
async: true,
dataType: 'json',
data: {
"array": ids
},
success:function(d){
}
后端代码:
String[] array = req.getParameterValues("array[]");
if (!StringUtils.isEmpty(array)){
for (String string : array) {
System.out.println("直接获取传递的数组参数:"+string);
}
}else{
System.out.println("直接获取传递的数组参数为空");
}