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.

Leave a Comment