后台代码
@RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
public JzBaseResponse deleteByIds(List<Integer> ids) {
menuService.deleteByIds(null);
return JzBaseResponse.setOk("删除成功");
}
@RequestMapping(value = "/deleteByIds2", method = RequestMethod.POST)
public JzBaseResponse deleteByIds2(@RequestBody List<Integer> ids) {
menuService.deleteByIds(null);
return JzBaseResponse.setOk("删除成功");
}
@RequestMapping(value = "/deleteByIds3", method = RequestMethod.POST)
public JzBaseResponse deleteByIds3(@RequestParam("ids") List<Integer> ids) {
menuService.deleteByIds(null);
return JzBaseResponse.setOk("删除成功");
}
@RequestMapping(value = "/deleteByIds4", method = RequestMethod.POST)
public JzBaseResponse deleteByIds4(DeleteMenuRequest deleteMenuRequest) {
menuService.deleteByIds(null);
return JzBaseResponse.setOk("删除成功");
}
@RequestMapping(value = "/deleteByIds5", method = RequestMethod.POST)
public JzBaseResponse deleteByIds5(@RequestBody DeleteMenuRequest deleteMenuRequest) {
menuService.deleteByIds(null);
return JzBaseResponse.setOk("删除成功");
}
@PostMapping(value = "/deleteByIds6")
public JzBaseResponse deleteByIds6(Integer[] ids) {
menuService.deleteByIds(null);
return JzBaseResponse.setOk("删除成功");
}
@PostMapping(value = "/deleteByIds7")
public JzBaseResponse deleteByIds7(@RequestParam(value="ids") Integer[] ids) {
menuService.deleteByIds(null);
return JzBaseResponse.setOk("删除成功");
}
@PostMapping(value = "/deleteByIds8")
public JzBaseResponse deleteByIds8(@RequestBody Integer[] ids) {
menuService.deleteByIds(null);
return JzBaseResponse.setOk("删除成功");
}
先测试第一种:&.post 不设置 $.ajaxSettings.traditional = true;
$.post('menu/deleteByIds', {ids: obj.ids}, function (res) {
layer.close(loadIndex);
if (res.code === 200) {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
});
$.post('menu/deleteByIds2', {ids: obj.ids}, function (res) {
layer.close(loadIndex);
if (res.code === 200) {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
});
$.post('menu/deleteByIds3', {ids: obj.ids}, function (res) {
layer.close(loadIndex);
if (res.code === 200) {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
});
$.post('menu/deleteByIds4', {ids: obj.ids}, function (res) {
layer.close(loadIndex);
if (res.code === 200) {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
});
$.post('menu/deleteByIds5', {ids: obj.ids}, function (res) {
layer.close(loadIndex);
if (res.code === 200) {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
});
$.post('menu/deleteByIds6', {ids: obj.ids}, function (res) {
layer.close(loadIndex);
if (res.code === 200) {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
});
$.post('menu/deleteByIds7', {ids: obj.ids}, function (res) {
layer.close(loadIndex);
if (res.code === 200) {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
});
$.post('menu/deleteByIds8', {ids: obj.ids}, function (res) {
layer.close(loadIndex);
if (res.code === 200) {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
});
结果:
1:
request:
以下2,3,4,5,6,7,8的请求request都同1
response:
2: response:
3: response:
4: response:
5: response:
6: response:
7: response:
8: response:
结论:
后端加了@RequestBody的都需要传输json格式(2,5,8) 而&.post默认传输的不是json格式 而是application/x-www-form-urlencoded
第六个请求成功,但后端实际没接收到值:
其他也都接受不成功,这种方法没有一个成功的。
第二种:还是&.post 但设置 $.ajaxSettings.traditional = true;
$.ajaxSettings.traditional = true;
$.post('menu/deleteByIds', {ids: obj.ids}, function (res) {
layer.close(loadIndex);
if (res.code === 200) {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
});
$.post('menu/deleteByIds2', {ids: obj.ids}, function (res) {
layer.close(loadIndex);
if (res.code === 200) {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
});
------不在累述-----
结果:
1:
request:
以下2,3,4,5,6,7,8的请求request都同1
response:
2
3
4
5
6
7
8
结论
3,4,6,7成功
没加 $.ajaxSettings.traditional = true时 传输参数是ids[] 加了之后是ids
2,5,8后端以json格式接收,前端不设置默认是application/x-www-form-urlencoded 不成功
3,4,6,7成功了 但第一个没成功啊 ,看报错信息:{“data”:null,“code”:“9999”,“msg”:“No primary or default constructor found for interface java.util.List”,“ok”:false}
但第三个加个@RequestParam(value=“ids”) 成功了 或者第四个外加一层
后端list方式接受前端数组 ,Content-Type:为 application/x-www-form-urlencoded 需要@RequestParam 或者实体类封装一下数组
后端以数组接收(6,7) 加不加RequestParam都一样
第三种 $.ajax方式 传输application/json
. a j a x 方 式 传 输 a p p l i c a t i o n / x − w w w − f o r m − u r l e n c o d e d 同 .ajax方式 传输application/x-www-form-urlencoded 同 .ajax方式传输application/x−www−form−urlencoded同.post
$.ajax({
type: 'POST',
data:obj.data ? JSON.stringify([obj.data.id]) : JSON.stringify(obj.ids),
contentType:"application/json",
url: 'menu/deleteByIds',
success: function (res) {
layer.close(loadIndex);
if (res.code === "0") {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
}
});
$.ajax({
type: 'POST',
data:obj.data ? JSON.stringify([obj.data.id]) : JSON.stringify(obj.ids),
contentType:"application/json",
url: 'menu/deleteByIds2',
success: function (res) {
layer.close(loadIndex);
if (res.code === "0") {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
}
});
---后面几个只是deleteByIds加了序号 不在累述 ---
后端没加@RequestBody的肯定也请求不同,就算请求成功了,也获取不到数据 比如4 6
只看 2 5 8 第5个干的久的都知道 应该也不行
结果:
request:
2:
response
5:
8
结论
2 8 成功 5失败
已json格式传输,后端@RequestBody Integer[] 或者 @RequestBody List 都可以
第四种 $.ajax方式 传输Content-Type: multipart/form-data
var formData= new FormData();
if(obj.data){
formData.append('ids',obj.data.id)
}else{
for(let k of obj.ids){
formData.append('ids',k)
}
}
$.ajax({
type: 'POST',
data: formData,
url: 'menu/deleteByIds',
processData: false,
contentType: false,
async: false,
success: function (res) {
layer.close(loadIndex);
if (res.code === "0") {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
}
});
$.ajax({
type: 'POST',
data: formData,
url: 'menu/deleteByIds2',
processData: false,
contentType: false,
async: false,
success: function (res) {
layer.close(loadIndex);
if (res.code === "0") {
layer.msg(res.msg, {icon: 1});
insTb.refresh();
} else {
layer.msg(res.msg, {icon: 2});
}
}
});
--------不在累述---------
结果:
request:
response
2 5 8 json排除
1
3 4 6 7成功 也获取到数据
1 不成功
结论
Content-Type: multipart/form-data和x-www-form-urlencoded结果一样
后端list方式接受前端数组 Content-Type:为 multipart/form-data 需要@RequestParam 或者实体类封装一下数组
后端以数组接收 加不加RequestParam都一样
综合结论
$.post 传输数组 默认x-www-form-urlencoded 需要设置$.ajaxSettings.traditional = true;
后端以Integer[]接收时加不加@RequestParam都可以 以List<Integer>接收时需要加@RequestParam
已封装类接受时不要加任何注解
$.ajax传输数组 默认x-www-form-urlencoded 需要设置$.ajaxSettings.traditional = true;
同$.pos
$.ajax传输数组 以json传输,可以@RequestBody Integer[] 或者@RequestBody List<Integer> ids
$.ajax传输数组 以multipart/form-data传输 结果同$.post的x-www-form-urlencoded
x-www-form-urlencoded需要设置$.ajaxSettings.traditional = true;
json和multipart/form-data不需要设置$.ajaxSettings.traditional = true;默认即可
不知道我总结的对不对啊 😂😂😂
补充:
前端以form提交: application/x-www-form-urlencoded
var checkRows =table.checkStatus('basicTable');
var ids=[];
if (checkRows.length !== 0) {
ids=checkRows.data.map(function (d) {
return d.id;
});
}
postExcelFile({ ids: ids},"/basic/exportDetail")
function postExcelFile(params, url) {
//params是post请求需要的参数,url是请求url地址
var form = document.createElement("form");
form.style.display = "none";
form.action = url;
form.method = "post";
document.body.appendChild(form);
// 动态创建input并给value赋值
for (var key in params) {
var input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = params[key];
form.appendChild(input);
}
form.submit();
form.remove();
}
请求:
后端:
@PostMapping(value = "/exportDetail")
public void exportDetail(@RequestParam(value = "ids") List<String> ids) {
basicService.exportDetail(ids);
}
或者
@PostMapping(value = "/exportDetail")
public void exportDetail(@RequestParam(value = "ids") String ids) {
basicService.exportDetail(ids);
}