java spring boot将字符串数组作为json中的参数传递给ajax调用
Posted
技术标签:
【中文标题】java spring boot将字符串数组作为json中的参数传递给ajax调用【英文标题】:java spring boot pass array of strings as parameters inside json for ajax call 【发布时间】:2015-09-30 20:50:52 【问题描述】:在我的应用程序中,我需要将一组参数从客户端传递到服务器端。我尝试了以下代码,但它不起作用。我需要从复选框列表中获取数据并将其传递到服务器端。 我的客户端代码
$(".add").click(function()
monitoring.length=0;
nonMonitoring.length=0;
$('.modal-body input:checked').each(function()
monitoring.push($(this).val());
);
$('.addkeywords input:checked').each(function()
nonMonitoring.push($(this).val());
);
// alert(monitoring[2]+ " " + nonMonitoring[2]);
var monitoringLength=monitoring.length;
var nonMonitoringLength=nonMonitoring.length;
$.ajax(
type : "GET",
url : '/rest/my/rest/mysql/controller',
data :
monitoringLength: monitoringLength,
nonMonitoringLength: nonMonitoringLength,
monitoring : monitoring,
nonMonitoring: nonMonitoring,
,
success : function(data)
// var keywordsList=data
//console.log(keywordsList);
// htm = "" ;
if(data=='success')
// loadChannels();
location.reload();
else
alert("failed to upload");
);
)
我的服务器端代码。
@RequestMapping("/rest/my/rest/mysql/controller")
public void monitorKeywords(@RequestParam(value="monitoringLength",required=true)int monitoringLength,@RequestParam(value="nonMonitoringLength",required=true)int nonMonitoringLength,@RequestParam(value="monitoring",required=true)List<String> monitoring,@RequestParam(value="nonMonitoring",required=true)List<String> nonMonitoring)
System.out.println("MonitoringLength =>" +monitoringLength);
System.out.println("NonMonitoringLength=>" +nonMonitoringLength);
System.out.println("Monitoring=>" +monitoring);
System.out.println("Monitoring=>" +nonMonitoring);
不知何故,这不起作用。这是什么错误?请帮忙
【问题讨论】:
【参考方案1】:在请求参数更改列表为数组
即
@RequestParam(value="monitoring",required=true) String[] monitoring, @RequestParam(value="nonMonitoring",required=true) String[] nonMonitoring
【讨论】:
以上是关于java spring boot将字符串数组作为json中的参数传递给ajax调用的主要内容,如果未能解决你的问题,请参考以下文章
从 POST 方法中使用 JDBCTemplate 存储对象数组 - Spring Boot
Spring Boot Mongo Upsert 数组中的元素