问题描述:前端Ajax向后台传输ids数组(F12查看负载确实有数据),但是后端一直接收不到,总是说后端类型不匹配。前端试过发送ids原类型和JSON类型,后端也对应的设置过接收对应的类型变量(后端注释的代码),但是依然报错类型不匹配。求大家指点一下。
追加:前端Ajax使用{'ids':ids}数据格式, 后端注解改为 @RequestMapping(value = "/deleteAll"), 方法参数改为public Result deleteAll(String[] ids) 起码不会直接报错停掉,但是拿到的ids为null.
case 'deleteAll':
var data = checkStatus.data;
var ids = new Array();
$(data).each(function () {
ids.push(this.id);
})
console.log('ids=',ids);
//[2,4]
layer.confirm(
'您确认要删除么?',
{icon: 3},
function (index) {
$.post(
'/product/deleteAll',
JSON.stringify({'ids': ids}), // 序列化为JSON字符串
// JSON.stringify(ids), // 序列化为JSON字符串
// {'ids':ids},
function (result) {
console.log(result);
if (result.code == 0) {
mylayer.okMsg(result.msg);
//删除成功之后刷新表格,展示最新数据
table.reload('tableId');
} else {
mylayer.errorMsg(result.msg);
}
},
'json'
);
}
);
break;
@RestController
@RequestMapping("/product")
public class ProductController {
@Autowired
private IProductService productService;
@RequestMapping(value = "/deleteAll", consumes = "application/json")
// public Result deleteAll(String[] ids){
public Result deleteAll(@RequestBody List<Integer> ids){
// public Result deleteAll(@RequestBody Map<String, List<Integer>> idsMap){
// public Result deleteAll(@RequestParam String[] ids){
// public Result deleteAll(@RequestParam Integer[] ids){
System.out.println("ProductController.deleteAll");
/*List<Integer> idList = Arrays.stream(ids)
.map(Integer::parseInt)
.collect(Collectors.toList());*/
// List<Integer> ids = idsMap.get("ids");
System.out.println("ids ================================================== " + ids);
// Integer deleteNum = productService.deleteAll(ids);
// System.out.println("deleteNum = " + deleteNum);
return Result.ok("Deletion completed");
}
}