Gene Parmesan
Gene Parmesan

Reputation: 102

Passing javascript array to a java servlet

I have an array that gets populated and then as I send it my servlet receives it as null.

var allIcons = new Array();
$('.icon').each(function(index){
  allIcons.push($(this).find('.iconName').html());
});

That seems to be filling up the array with the appropriate fields

I am then passing it to my servlet using

$.ajax({
   "dataType" : 'json',
   "type": 'GET',
   "url" : 'update'
   "data" :{
      "allIcons" : allIcons
   }, "success": function(json){alert("alert");}});

My servlet is then attempting to read it but always gets back null

if(request.getParamtersValues("allIcons").length > 0) {/*do something*/}

request.getParamterValues() should return a String[]

In addition I know my servlet is able to receive data since this is in addition to some other code. Thanks -Tommy

Upvotes: 0

Views: 3013

Answers (2)

007
007

Reputation: 161

$.ajax({
   dataType : 'json',
   type: 'GET',
   url : 'update'
   data :{
      "allIcons" : allIcons
   }, success: function(data){alert("alert");}});

Upvotes: 1

Gene Parmesan
Gene Parmesan

Reputation: 102

The request.getParameterNames() returned me "allIcons[]" where I was looking for "allIcons" without the brackets. So, if you are looking for an array coming from JS to a servlet be sure to use the right parameters. Be sure to add your braces. "[]"

Upvotes: 0

Related Questions