7

I'm doing a simple AJAX post using jQuery, works great:

var parameters = {firstName: 'John', lastName: 'Smith'};
$.post('http://api.example.com/rest', parameters, function(data) {
  alert('Response: ' + data.someResult);
});

However, when I add an array to the parameters like so:

var parameters = {firstName: 'John', lastName: 'Smith', children: ['Susy', 'Billy']};

Then the problem is the parameter name children gets changed to children[] (it's actually URL encoded to children%5B%5D) when POSTing to the server. I can't change the server to look for parameters with the name children[] so what do I do? How can I POST multiple values with the name children? Why is jQuery changing the name of my parameter?

1 Answer 1

6

I believe you need to enable traditional parameter encoding.

See http://api.jquery.com/jQuery.ajax/ and http://api.jquery.com/jQuery.param

As $.post doesn't have a specific option for this you'll either need to revert to $.ajax or use the global setting jQuery.ajaxSettings.traditional = true.

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

2 Comments

Ok thanks! I'm concerned that setting a global value like that might interfere with other 3rd party scripts on my webpages? So I'll just use the $.ajax mechanism.
You can also call jQuery.param(parameters, true), passing the result where you currently pass raw parameters: jQuery.param()

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.