Java spring boot array of strings as parameters inside json for ajax call

In my application, I need to pass an array of parameters from the client side to the server. I tried the following code but it doesn't work. I need to get data from a list of checkboxes and pass it to the server. My client side code

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

                }


            }


});



})

      

My code is for the server side.

@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);

      

Somehow it doesn't work. What's the mistake in this? Please, help

+3


source to share


1 answer


In the parameter "Edit Query" List for Array

i.e.



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

      

+3


source







All Articles