list = JSON.stringify(list);
				// layer.msg(JSON.stringify(list))
                layer.confirm('确认要删除吗?'  , function(index) {
                    //捉到所有被选中的,发异步进行删除
                    $.ajax({
                        type:'post',
					    url:"/people/deleteAllPeople",
					    data:{list: list},
                        contentType : 'application/json',

                        success:function (data) {
							if(data.code==200)
							{
                                $(".layui-form-checked").not('.header').parents('tr').remove();
							}
                            layer.msg(data.msg, {
                                icon: 1
                            });
					    }
                    });

 

  @RequestMapping("deleteAllPeople")

    public Response delAll(@RequestBody List<People> list ){
        for (int i = 0; i <list.size() ; i++) {
           if( peoplerService.delete(list.get(i).getId())==null)
               return Response.failure(Msg.UPD_FALIED);
        }

            return Response.success(Msg.UPD_SUCCESS);

    }

然后报错:Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'list': was expecting ('true', 'false' or 'null'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'list': was expecting ('true', 'false' or 'null')
 at [Source: (PushbackInputStream); line: 1, column: 6]]

字符串解析错误,于是找了很久终于找道问题所在,在于 ajax 的data 要传的是 data:list   而不是 data:{list:list},下面是修改后的

 list = JSON.stringify(list);
				// layer.msg(JSON.stringify(list))
                layer.confirm('确认要删除吗?'  , function(index) {
                    //捉到所有被选中的,发异步进行删除
                    $.ajax({
                        type:'post',
					    url:"/people/deleteAllPeople",
					    data:list,
                        contentType : 'application/json',

                        success:function (data) {
							if(data.code==200)
							{
                                $(".layui-form-checked").not('.header').parents('tr').remove();
							}
                            layer.msg(data.msg, {
                                icon: 1
                            });
					    }
                    });

总结:

1.首先ajax要为post,

2.参数只能是json,data:list

3 设置      contentType : 'application/json',不然会Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported] ,因为默认这种类型不能被解析

4.数组要JSON.stringify(list);解析成为一个合法的json字符串

5.controller 加上@RequestBody

Logo

助力广东及东莞地区开发者,代码托管、在线学习与竞赛、技术交流与分享、资源共享、职业发展,成为松山湖开发者首选的工作与学习平台

更多推荐