@
ukipoi #8 两种方式:
1. 后端使用 `application/json` 格式,参数用 `@RequestBody` 映射
```
@
PostMapping(
consumes = MediaType.APPLICATION_JSON_VALUE
produces = MediaType.APPLICATION_JSON_VALUE
)
public Response blahahah(@RequestBody YourJsonPayloadObject yourJsonPayloadObject) {
...
}
```
前端:
```
$.ajax({
url: '...',
method: 'POST',
contentType: 'application/json',
dataType: 'json',
processData: false,
data: JSON.stringify({
imgs: ['IMGURL1', 'IMGURL2'],
...
}),
})
```
2. 使用 `application/x-www-form-urlencoded` post 数据
如果使用 `traditional: true`
```
$.ajax({
url: '...',
method: 'POST',
traditional: true,
data: {
imgs: ['IMGURL1', 'IMGURL2'],
...
},
})
```
会产生这样的 payload: `imgs=IMGURL1&imgs=IMGURL2`,后端不用改
如果使用 `traditional: false`(默认)
```
$.ajax({
url: '...',
method: 'POST',
data: {
imgs: ['IMGURL1', 'IMGURL2'],
...
},
})
```
会产生这样的 payload: `imgs[]=IMGURL1&imgs[]=IMGURL2`,
后端的 `@RequestParam("")` 要改为 `@RequestParam("imgs[]")`