1

For my application I am writing a POST request to send array of parameters from a checkbox list. Its working for get request but not working for post request. What is the error in my code.

My code on the client side for sending ajax request to the server.

$(".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 : "POST",
            url : '/rest/channelstats/my/rest/controller',
            data : {
            //  monitoring : monitoring,
            //  nonMonitoring: nonMonitoring,
                monitoringLength: monitoringLength,
                nonMonitoringLength: nonMonitoringLength,

            },
            success : function(data) {

            //  var keywordsList=data
                //console.log(keywordsList);
            //  htm = "" ;


            }


});


    })

My java code on the server side.

@RequestMapping(value="/rest/channelstats/my/rest/controller",method = RequestMethod.POST)
public void monitorKeywords(@RequestParam(value="monitoringLength",required=true)int monitoringLength,@RequestParam(value="nonMonitoringLength",required=true)int nonMonitoringLength){
    System.out.println("MonitoringLength =>" +monitoringLength);
    System.out.println("NonMonitoringLength=>" +nonMonitoringLength);

}

}

Its working for HTTP GET requests but not working for POST requests.How should I solve this problem?

Niranjan Dattatreya
  • 405
  • 1
  • 6
  • 18

1 Answers1

0

According to your jquery post request, you should use DAO(Data Access Object) to parse the request data. So you should add class Request

public class Request {

    private int monitoringLength;
    private int nonMonitoringLength;

    //getters and setters
} 

And change controller to

@RequestMapping(value="/rest/channelstats/my/rest/controller",method = RequestMethod.POST)
public void monitorKeywords(@RequestBody Request request){
    System.out.println("MonitoringLength =>"+request.getMonitoringLength());            
    System.out.println("NonMonitoringLength=>"+request.getNonMonitoringLength());
}
Community
  • 1
  • 1
Loniks
  • 489
  • 1
  • 8
  • 19