3

I am sending an Array of values through Ajax via jQuery with a Play Framework backend, and I'm in front of a problem.

Here's an example :

$.ajax ({
    'type':     'POST',
    'url':          '/url',
    'timeout':  5000,
    'data':     {'ids': [0, 1, 2, 3]},
    'dataType': 'json',
    'success':  function (oData) {
        // Process ...
    }
});

But in Play!, if I do a params.get("ids");, I got an empty value, and if I do a params.getAll("ids"); also.

I know where the problem is, jQuery send the data as : ids[]=0&ids[]=1&ids[]=2&ids[]=3 but Play! Framework expect array data to be sent as ids=0&ids=1&ids=2&ids=3

Is there a proper way to send the data properly (or get the data as an array in my controller) ?

So far, I managed to make it works simply but creating the request as a String manually in JavaScript.

3
  • Can you set the data as 'data': {'ids': 0,'ids': 1,'ids': 2, 'ids': 3}? Commented Jan 13, 2012 at 18:47
  • @SKS, have you tried that? You would end up with a map of just { 'ids': 3 } since you are wiping out ids with each map entry. Commented Jan 15, 2012 at 17:26
  • @marchaos: True, see my post below.. I was suggesting to write a js function to manually set the query string :( Commented Jan 16, 2012 at 1:34

4 Answers 4

4

One method (leaving your JavaScript code intact) is just declaring your controller method like this:

public static void myMethod(@As("ids[]:")List<Long> ids) {
    System.out.println(ids.get(0));
}

.. the output is what you expect:

[0, 1, 2, 3]

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

1 Comment

That's nice! It's just sad there isn't a "natural" way to do it with Play! Maybe I should open a ticket to ask them that. Thanks for that idea.
1

I'm not sure if there's an easier way, but you could just send a string and decode that using GSON, i.e:

$.ajax ({
    'type':     'POST',
    'url':          '/url',
    'timeout':  5000,
    'data':     {'ids': '[0, 1, 2, 3]'},
    'dataType': 'json',
    'success':  function (oData) {
        // Process ...
    }
});

Within your controller, you can convert the string into an array:

// data would be "[0, 1, 2, 3]"
int[] intArray = gson.fromJson(data, int[].class);

Comments

1

This solution worked for me. Give it a try,

// Sending request
var params = {
    myArray: [1, 2, 3, 4]
};
var url = '@controllers.routes.AppController.jquery()';
$.post(url, params, function (data) {
    // Process response here
});

// Receiving request
Map<String, String[]> params = request().body().asFormUrlEncoded();
Logger.debug("Params : " + params.size());
for (Map.Entry<String, String[]> param : params.entrySet()) {
    Logger.debug(param.getKey() + " = " + param.getValue()[0]);
}

Comments

0

Just add traditional: true to your jQuery.ajax request and the Array will be sent as ids=0&ids=1&ids=2&ids=3

$.ajax ({
    traditional: true,
    type:        'POST',
    url:         '/url',
    timeout:     5000,
    data:        {'ids': [0, 1, 2, 3]},
    dataType:    'json',
    success:     function (oData) {
        // Process ...
    }
});

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.