使用 ajax 传数组,可以有很多种方式实现,如将数组序列化成 json字符串,或者将数组转换成字符串,都能实现。这里介绍一种方式,添加属性 traditional: true,直接传
代码如下
controller
package com.approval.controller;import com.approval.param.SendListParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** DemoController** @author wsjz* @date 2022/01/13*/
@RestController
public class DemoController {@RequestMapping("/send/list")public String sendList(SendListParam param) {System.out.println(param.getName());param.getProjectIds().forEach(System.out::println);return "success";}
}
参数实体类
package com.approval.param;import lombok.Data;
import java.util.List;/*** SendListParam** @author wsjz* @date 2022/01/13*/
@Data
public class SendListParam {private String name;private List<String> projectIds;
}
ajax
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><script src="js/jquery-3.2.1.min.js"></script>
</head>
<body><button id="send">发送</button><script>$("#send").click(function () {var name = '小红'var projectIds = ['1', '3', '12', '3']$.ajax({type:"post",url:"/send/list",traditional: true,data: {name: name,projectIds: projectIds},async:true,success:function(res){alert(res)},error: function (xhr, textStatus, errorThrown) {}});})</script>
</body>
</html>
测试

至此完















