How to send request parameter array to servlet using jQuery $.ajax? How to send request parameter array to servlet using jQuery $.ajax? ajax ajax

How to send request parameter array to servlet using jQuery $.ajax?


Send array as value of JS object so you end up as {json:[1,2,3,4]}.

var json=[1,2,3,4];$.ajax({    url:"myUrl",    type:"POST",    dataType:'json',    data: {json:json},    success:function(data){        // codes....    },});

In servlet, you need to suffix the request parameter name with [].

String[] myJsonData = request.getParameterValues("json[]");

jQuery appends them in order to be friendly towards weak typed languages like PHP.


You have to convert your array to a JSON type so instead of [] it needs to read

 var array = [ 1, 2, 3, 4 ];

to do this you need to call

 var json = JSON.stringify(array)

then you can pass it into your ajax call

 $.ajax({ url:"myUrl",          type:"POST",          data: json,          dataType:'json',          success:function(data){             // codes....          }})


Try using below script -

 jQuery.ajax({                    url : "your API",                    type : "POST",                    dataType:'json',                    data: JSON.stringify({ jsonData: data }),                    contentType: "application/json",                    success : function(response) {    //do the needful.    },                    error : function(jqXHR, textStatus,                            errorThrown) {                        var x = 1;                        closeLoader();                      }                });

handle the request in the controller as below -

@RequestMapping(value="your url", method = RequestMethod.POST)public Map<String, Object> verifyRefundRequested(@RequestBody String data) throws UnsupportedEncodingException{        Map<String, Object> responseMap = null;        Gson g = new Gson();        responseMap = g.fromJson(data, Map.class);        List<String> s = (List<String>) responseMap.get("jsonData");//iterate list and process // return map        }