方法一:
1. 使用 JSON.stringify 将数组对象转化成 json 字符串;
- var array = ["1", "2"];
- $.ajax({
- type : 'POST',
- url: path + '/check/testPost',
- contentType : "application/json" ,
- data : JSON.stringify(array),
- success : function(data) {
- }
- });
2. 传输过程中参数
这里写图片描述
3. 后台处理
- @RequestMapping(value = "/testPost", method = {RequestMethod.POST})
- public void testPost(@RequestBody String[] array) throws IOException {
- for (String string : array) {
- System.out.println(string);
- }
- return ;
- }
方法二:
1. 前端不做处理:
- var array = ["1", "2"];
- $.ajax({
- type : 'POST',
- url: path + '/check/testPost',
- contentType: "application/x-www-form-urlencoded",
- data: {"array": array},
- success : function(data) {
- }
- });
2. 传输过程中参数
这里写图片描述
3. 后台处理
- @RequestMapping(value = "/testPost", method = {RequestMethod.POST})
- public void testPost(HttpServletRequest req) throws IOException {
- String[] array = req.getParameterValues("array[]");
- for (String string : array) {
- System.out.println(string);
- }
- return ;
- }
注: 两种 post 请求的 content-type 不同.
来源: http://www.qdfuns.com/article/18084/e1b863fc1e29a2dc4391c774cd810a79.html