18

I would like to send JavaScript array to servlet using jQuery $.ajax.

var json=[1,2,3,4];
$.ajax({
            url:"myUrl",
            type:"POST",
            dataType:'json',
            success:function(data){
                // codes....
            },
            data:json

        });

When I use

request.getParameter("json");
request.getParameterValues("json");

It returns null.

How can I access the values?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
IbrahimAsad
  • 516
  • 1
  • 5
  • 16
  • 1
    data is a JSON object so it must be like data: {name value pair, where value could be a object} – Dhruvenkumar Shah Nov 05 '12 at 22:58
  • 1
    I don't see anywhere that you define a parameter named _json_ so I'm not sure why you'd expect _getParameter("json")_ to return anything other than null. – jahroy Nov 05 '12 at 22:59
  • Are you trying to post to a server side file? to store the data in a database for example? –  Nov 05 '12 at 22:59

4 Answers4

36

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.

Community
  • 1
  • 1
Raunak Agarwal
  • 7,117
  • 6
  • 38
  • 62
0

You have to convert your array to a JSON type so instead of [] it needs to read

 var array = [ 1, 2, 3, 4 ];

to do this you need to call

 var json = JSON.stringify(array)

then you can pass it into your ajax call

 $.ajax({ url:"myUrl",
          type:"POST",
          data: json,
          dataType:'json',
          success:function(data){
             // codes....
          }})
DWolf
  • 703
  • 1
  • 7
  • 20
0

Try using below script -

 jQuery.ajax({
                    url : "your API",
                    type : "POST",
                    dataType:'json',
                    data: JSON.stringify({ jsonData: data }),
                    contentType: "application/json",
                    success : function(response) {
    //do the needful.
    },
                    error : function(jqXHR, textStatus,
                            errorThrown) {
                        var x = 1;
                        closeLoader();  
                    }
                });

handle the request in the controller as below -

@RequestMapping(value="your url", method = RequestMethod.POST)
public Map<String, Object> verifyRefundRequested(@RequestBody String data) throws UnsupportedEncodingException{
        Map<String, Object> responseMap = null;
        Gson g = new Gson();
        responseMap = g.fromJson(data, Map.class);
        List<String> s = (List<String>) responseMap.get("jsonData");
//iterate list and process 
// return map
        }
-1

You need to post your javascript data object like this..

http://api.jquery.com/jQuery.post/

$.post("test.php", { name: "John", time: "2pm" },
   function(data) {
     alert("Data Loaded: " + data);
   });
Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66