How do you send an array as part of an (jquery) ajax request

Posted by Ankur on Stack Overflow See other posts from Stack Overflow or by Ankur
Published on 2011-01-11T10:01:54Z Indexed on 2011/01/11 12:53 UTC
Read the original article Hit count: 120

Filed under:
|
|
|
|

I tried to send an array as part of an ajax request like this:

var query = [];
// in between I add some values to 'query'
$.ajax({
    url: "MyServlet", 
    data: query,
    dataType: "json",  
    success: function(noOfResults) { 
    alert(noOfResults); 
    }
  });
}

I wanted to see what I get back in the servlet, so I used this line:

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

Which returned {} suggesting an empty map.

Firebug tells me I am getting a 400 bad request error

If I send a queryString like attribute=value as the 'data' then everything works fine, so it has to do with not being able to send an array as is. What do I have to do to get that data into the servlet for further processing. I don't want to pull it out and turn it into a queryString in the JS if I can avoid it.

EDIT: I used the .serializeArray() (jQuery) function before sending the data. I don't get the 400 but nothing useful is being sent through.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery