Hemachandra Hemachandra - 4 months ago 20
JSON Question

How to pass array in jquery ajax call and how to receive same from spring mvc controller

I am calling spring mvc controller method using jquery ajax.

jquery ajax call:

var filters = {KPI : visual, partnerName: params['partnerName'], itemQualifier: getItemQualifier(), currency : getCurrency(), sellSide: getSellSide()};

$.ajax({
type: "POST",
url: "/reports/savefilters",
data: filters,
success: function(response){
alert('success: ' + response);
},
error: function(e){
alert('Error: ' + e.message);
}
});


Spring mvc code:

@RequestMapping(value="/savefilters",method=RequestMethod.POST)
public void saveFilters(@RequestParam("KPI") String KPI, @RequestParam("partnerName") String partnerName,
@RequestParam("itemQualifier") String itemQualifier, @RequestParam("currency") String currency,
@RequestParam("sellSide") String sellSide){
System.out.println("saveFilters");
}


Its working fine without "@RequestParam("partnerName") String partnerName" parameter.

Here "params['partnerName']" values are like abc,def,ghi.

Am I not sending data properly to controller???
Any suggestions how to make it work???. Thanks...

Answer

Change

@RequestParam("partnerName") String partnerName

to

@RequestParam(value = "partnerName[]", required = false) String[] partnerName
Comments