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?

3
  • 1
    data is a JSON object so it must be like data: {name value pair, where value could be a object} Commented Nov 5, 2012 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. Commented Nov 5, 2012 at 22:59
  • Are you trying to post to a server side file? to store the data in a database for example? Commented Nov 5, 2012 at 22:59

4 Answers 4

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.

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

1 Comment

Made a mistake on the server side you may have to use.. request.getParameterValues("json[]");
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....
          }})

Comments

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
        }

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.