【问题标题】:java spring boot pass array of strings as parameters inside json for ajax calljava spring boot将字符串数组作为json中的参数传递给ajax调用
【发布时间】: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);

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

【问题讨论】:

    标签: java ajax json spring model-view-controller


    【解决方案1】:

    在请求参数更改列表为数组

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

    【讨论】:

      猜你喜欢
      • 2012-07-26
      • 1970-01-01
      • 2011-02-12
      • 2015-01-12
      • 2010-10-03
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多