异步请求
将 Array 转换为json字符串 : JSON.stringify(bugTypeArray);
未转换字符串会如下报错:
org.springframework.http.converter.HttpMessageNotReadableException:
JSON parse error: Unrecognized token 'bugTypeArray': was expecting ('true', 'false' or 'null');
nested exception is com.fasterxml.jackson.core.JsonParseException:
Unrecognized token 'bugTypeArray': was expecting ('true', 'false' or 'null')
使用json传输数据: contentType: “application/json;charset=UTF-8”
未设置请求类型会如下报错:Content type ‘application/x-www-form-urlencoded;charset=UTF-8’ not supported
var bugTypeArray = new Array();
//获取选中的数据
$("input[name='btSelectItem']:checked").each(function(){
var bugType = $(this).parent().next().next().text();
bugTypeArray.push(bugType)
});
//异步请求
$.ajax({
type:"post",
url:"countNumber",
data:JSON.stringify(bugTypeArray), //将数据转json字符串
contentType: "application/json;charset=UTF-8", //json格式传输数据
dataType:"json",
success:function(data){
$(".bootstrap-table button[name='refresh']").click();
swal("更新成功!");
},
error:function(){
swal("处理失败!");
}
});
处理接受的参数
使用 @RequestBody注解 将json字符串转换为java集合
@ResponseBody
public Object countNumber(@RequestBody List<String> bugTypeArray) {
logger.info("size === " + bugTypeArray.size());
return bugService.updateCountNumber(bugTypeArray);
};