gpt4 book ai didi

java spring boot将字符串数组作为json中的参数传递给ajax调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:42:42 27 4
gpt4 key购买 nike

在我的应用程序中,我需要将一组参数从客户端传递到服务器端。我尝试了以下代码,但它不起作用。我需要从复选框列表中获取数据并将其传递到服务器端。我的客户端代码

$(".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);

不知何故这不起作用。这是什么错误?请帮忙

最佳答案

在Request参数中将List改为array

@RequestParam(value="monitoring",required=true) String[] monitoring, @RequestParam(value="nonMonitoring",required=true) String[] nonMonitoring

关于java spring boot将字符串数组作为json中的参数传递给ajax调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31368989/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com