1

If a html form has multiple inputs with the same name, a Servlet will understand it as Array:

Form:

<form>
    User 1:<input name="user" />
    User 2:<input name="user" />
    User 3:<input name="user" />
    <input type="submit" />
</form>

Servlet:

String[] user = request.getParameterValues("user");

But if I use jQuery to make an ajax call, and send a array as a parameter, it adds "[]" in the property name

Javascript:

$.ajax({
    ...
    data:{
        user: ["value1", "value2", "value3"]
    }
});

Inspecting the header of the request, the data were sent:

Form Data

user[]=value1&user[]=value2&user[]=value3

I know I can add [] at the end of parameter of getParameterValues, but I prefer that jQuery does not send the []

How can I do this?

0

1 Answer 1

2

You need to set the traditional option to true if you wish to use the traditional style of param serialization:

$.ajax({
    ...
    data:{
        user: ["value1", "value2", "value3"]
    },
    traditional: true
});

Using this your request will look like:

user=value1&user=value2&user=value3
Sign up to request clarification or add additional context in comments.

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.