Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AJAX post with jQuery changing the name of an array parameter

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?

like image 892
at. Avatar asked May 04 '11 18:05

at.


1 Answers

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.

like image 53
Alnitak Avatar answered Oct 14 '22 22:10

Alnitak