方法1:前端传 JSON 字符串,后端再解析 JSON。
前端
let ids = [1, 2, 3]
post('/api/test/', {
category: 1,
ids: JSON.stringify(ids) // 使用 JSON
})
...
import axios from 'axios'
import qs from 'qs'
axios.defaults.timeout = 10000
var header = {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
}
export function post (url, params, headers = header) {
return new Promise((resolve, reject) => {
axios.post(url, qs.stringify(params), {
headers: headers
}).then(res => {
resolve(res.data)
}).catch(err => {
reject(err.data)
})
})
}
后端
@PostMapping("/test")
public String test(@RequestParam("category") int category,
@RequestParam("ids") String idsStr) {
System.out.println(category);
List<Integer> ids = JSON.parseArray(idsStr, Integer.class);
System.out.println(ids);
return "ok";
}
1
[1, 2, 3]
方法2:使用 @RequestBody
前端
post2('/api/test/', [{
account: '11111111111',
password: '000000'
}, {
account: '11111111111',
password: '000000'
}], {
'Content-Type': 'application/json'
})
export function post2 (url, params, headers = header) {
return new Promise((resolve, reject) => {
axios.post(url, params, {
headers: headers
}).then(res => {
resolve(res.data)
}).catch(err => {
reject(err.data)
})
})
}
后端
@PostMapping("/test")
public String test(@RequestBody List<User> users) {
System.out.println(users);
return "ok";
}
public class User {
private int id;
private String account;
private String password;
...
}
来源:CSDN
作者:W.T.F.
链接:https://blog.csdn.net/fcku_88/article/details/103850399