6

I am using JQuery's post(), but my servlet doesn't see the array that I'm passing as a parameter.

My javascript looks like this:

var myArray = ['1', '2'];
$.post('action.do', {"arrayData":myArray, "mode":"insert"});

In my servlet:

System.out.println(request.getParameterMap());

which outputs:

{mode=insert}

I've also tried

$.post('action.do', {"arrayData[]":myArray, "mode":"insert"});

and

$.post('action.do', {"arrayData":$(myArray).serializeArray(), "mode":"insert"});

3 Answers 3

6

I had this problem. I resolved it just by adding brackets to "arrayData" parameter server-side. On client:

$.post('action.do', {arrayData:myArray, mode:"insert"});

Please, note, on the client-side arrayData parameter is without brackets.

On server:

String[] arrayData=request.getParameterValues("arrayData[]");

This worked for me!

Sign up to request clarification or add additional context in comments.

Comments

0

Try to use

$.post('action.do', {"arrayData":myArray, "mode":"insert"});

and on server

String[] arrayData=request.getParameterValues("arrayData");

1 Comment

For this to work, the parameter map would print something like {mode=insert, arrayData=java.lang.String@3sD39} or something similar. The servlet is not receiving the arrayData parameter at all, as a string or string array or any other sort of object.
0

Here is how I pass json to my servlet, using json2.js from here. In the servlet you can then use gson, or jackson to automatically convert you json to an instance of suitable java class autonoumously.

var jsonData = $("#myform").toObject();
var strJson = JSON.stringify(jsonData);
$.ajax({
  cache:false,
  type: 'POST',
  url: myUrl,
  data:strJson,
  contentType: "application/json",
  success:  function(data) {            
          //mySuccessHanlder
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.