Pase elementos de matriz como múltiples parámetros en jQuery AJAX POST

I have an array of id's which I wish to pass as a parameter to a webservice via jQuery's $.post like so:

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

$.post('/save', {ids: selections, function(data) {
    // do something
});

Unfortunately the code above submits multiple parameters (which is what I want) but it appends a '[]' to the parameters name so the params sent through when inspected with firebug look like:

ids[] 1
ids[] 2
ids[] 3
ids[] 4

The size of the array is dynamic so I can't hardcode it. Is there a dynamic way to do this without creating a parameter string manually?

Gracias, Gearoid.

EDIT:

I should also mention that I'm submitting to a Jersey Webservice method which looks like the following:

@POST 
@Path("save")
@Produces({"application/xml","application/json"})
@Consumes("application/x-www-form-urlencoded")
public Response save();

preguntado el 08 de noviembre de 11 a las 17:11

5 Respuestas

Here is an example of dynamically creating a paremeter string using jQuery: http://jsfiddle.net/rre47/4/

var arr = {'ids' : []};//create new object (empty)

The basic idea is to create an object with an array under the 'ids' key. Then iterate through your data to append data to this array:

arr.ids.push(<some value>);

Then to output the parameter string:

decodeURIComponent($.param(arr));

respondido 08 nov., 11:21

Maybe the jQuery function $.param() es lo que necesitas:

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

respondido 08 nov., 11:21

Use JSON encoding:

var selections = [1,2,3,4];
$.post('/save', {ids: selections}, function(data) {
    // do something
},
"json");

Desde jQuery's post

This would create a string: '{"ids": [1,2,3,4]}' and send that to the server.

And you seem to have a typo in your question:

$.post('/save', {ids: selections, function(data) {

debiera ser:

$.post('/save', {ids: selections}, function(data) {

respondido 08 nov., 11:21

Use traditional AKA "shallow" serialization as described here http://api.jquery.com/jQuery.param/.

The result at the first "level" will be without the brackets.

$.ajax({
    url : '/save',
    data : {
        ids: selections
    },
    traditional : true
});

Respondido 15 Oct 14, 00:10

I would simply use the Array method .join to create a string out of the array, and then use PHP's explode function to convert it back into an array:

JS:

var selectionsString = selections.join('|'); //returns '1|2|3|4'

$.post('/save', {ids: selectionsString, function(data) {
    // do something
});

PHP:

$selectionsArray = explode('|', $_POST['ids']); //returns [1,2,3,4]

respondido 08 nov., 11:21

Fine Gearoid. But surely there is a similar method for whatever you ARE using, or one could be created without too much trouble. - Maxedison

Of course, but you're also suggesting I put client specific code in my service layer. You're suggesting I write code in my API that handles different input formats which isn't the API's job. Assume that I'm not even allowed to touch the web service code. - Gerard

Why should I assume these things? You never said them until now. - Maxedison

So you'd like me to write "Assume I want to keep my server side code loosely coupled from my client side?" - Gerard

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.