开发者

Sending an array with ajax to Java on AppEngine

开发者 https://www.devze.com 2023-03-26 18:26 出处:网络
I am currently using Java on AppEngine. I have an array of strings in javascript. I would like to send this array to the server using jQuery and the ajax() function.

I am currently using Java on AppEngine.

I have an array of strings in javascript. I would like to send this array to the server using jQuery and the ajax() function.

How can I send the array so that on the Java server side I will be able to convert it to a List<String> instance?


EDIT:

In th开发者_JAVA百科e client side I've used this code (jQuery):

      var namesArray = []
      namesArray[0] = "a"
      namesArray[1] = "b"
      $.ajax({
          type: "POST",
          url: "/ajax/someURL",
          data: {
              namesArray: namesArray
          }
      });

When I debug the server side I can see that HttpServletRequest instance _parameters is:

{namesArray[]=[a,b]}

But when I use req.getParameter("namesArray[]") it returns me the string "a"... Like stated above, my goal is to get a List of strings (List<String>) containing both "a" and "b".


I'm not too sure about the Java side of things, but if you want to pass the javascript array to the server as an array encoded in a POST variable, you could do:

var myarray = ['Element 1', 'Element 2', 'Element 3'] ;
var dataobject = {
    postvar: myarray
} ;
$.ajax({
    url: 'http://url.of/your/server/code',
    data: dataobject,
    type: 'POST'
}) ;

The POST variable in this case would be 'postvar'.

0

精彩评论

暂无评论...
验证码 换一张
取 消