0

If data1 and data2 are javascript arrays

e.g ["aa","bb"] and ["xx","yy"]

  $.ajax({
    url : 'testServlet',
    type: 'post',
    data : 
        [{"array1" : data1},
         {"array2" : data2}]
    ,
    success : function(responseText) {
        //...
    }
});

how can I retrieve it from the testServlet??

or does my data have to be in the form of json format?(not familiar with this part)

I have tried using

 String[] data= request.getParameterValues("array1");

but it is null

securenova
  • 482
  • 1
  • 9
  • 25
  • Use `request.getReader()` to get the data. – Ean V Mar 19 '17 at 11:17
  • Possible duplicate of [Correct way for parsing JSON objects containing arrays in Java Servlets (with Gson for example)](http://stackoverflow.com/questions/9829819/correct-way-for-parsing-json-objects-containing-arrays-in-java-servlets-with-gs) – Nishikant Nipane Mar 19 '17 at 11:37

2 Answers2

0

You can print out the request parameters and their values with the following snippet:

Enumeration params = httpRequest.getParameterNames();
while(params.hasMoreElements()){
    String paramName = (String)params.nextElement();
    System.out.println(paramName + " = " + httpRequest.getParameter(paramName));
}

This should print all the parameters and values. This will help you check/debug what's retrieved at server side.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
0
var test = [{"array1" : data1},
         {"array2" : data2}];

$.ajax({
    type: 'post',
    url: 'testServlet',
    dataType: 'JSON',
    data: { 
      test: JSON.stringify(test)
    },
    success : function(responseText) {
    //...
}
});

And, on testServlet

String json = request.getParameter("test");
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • thanks , but I was hoping to be able to retrieve the elements in the 2 arrays, but now I,m just getting a String like this [{"array1":["aa","bb"]},{"array2":["xx","yy"]}] ? – securenova Mar 19 '17 at 11:59
  • there is difference in array and json. In your question, it is conflicting, you showed array but passing it as json. – Ravi Mar 19 '17 at 12:16