$ .post () javascript array for servlet

I am using jQuery post() , but my servlet does not see the array that I pass 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 = insertion}

I also tried

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

and

 $.post('action.do', {"arrayData":$(myArray).serializeArray(), "mode":"insert"}); 
+4
source share
3 answers

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

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

Note that on the client side, the arrayData parameter arrayData not contain parentheses.

On server:

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

It worked for me!

+3
source

Try using

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

and on the server

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

This is how I pass json to my servlet using json2.js from. In a servlet, you can use gson or jackson to automatically convert you json into an instance of a suitable java class automatically.

 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 } }); 
0
source

Source: https://habr.com/ru/post/1390804/


All Articles