如何将整数数组传递给弹簧控制器? [英] How do I pass an array of integer into spring controller?
本文介绍了如何将整数数组传递给弹簧控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
script Array
of int
我希望传入Spring Controller
.但我不断得到
script Array
of int
and I wish to pass into Spring Controller
. but I keep getting
400 bad request.
如果我的 js 数组
是
array = [1,2,3,4]
array -> 400 bad request
JSON.Stringify(array) -> I will get [1,2,3,4]
$.ajax({//jquery ajax
data:{"images": array},
dataType:'json',
type:"post",
url:"hellomotto"
....
})
当我循环 string List
.. 第一个元素将是 '[1'
when I loop the string List
.. the first element will be '[1'
@RequestMapping(value = "/hellomotto", method = Request.POST)
public void hellomotto(@RequestParam("images") List<String> images){
sysout(images); -> I will get [1,2,3,4]
}
公共无效
我可以知道如何正确执行此操作吗?我尝试了不同的组合
May I know how can I do this properly? I have tried different combination
推荐答案
以下是一个工作示例:
Javascript:
Javascript:
$('#btn_confirm').click(function (e) {
e.preventDefault(); // do not submit the form
// prepare the array
var data = table.rows('.selected').data();
var ids = [];
for(var i = 0; i < data.length; i++) {
ids.push(Number(data[i][0]));
}
$.ajax({
type: "POST",
url: "?confirm",
data: JSON.stringify(ids),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
alert(data);
},
failure: function(errMsg) {
alert(errMsg);
}
});
});
控制器:
@RequestMapping(params = "confirm", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody int confirm(@RequestBody Long[] ids) {
// code to handle request
return ids.length;
}
这篇关于如何将整数数组传递给弹簧控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文