Arun Stephens

jQuery, posting arrays and square brackets

This is old, but new to me. I guess it shows that I haven’t been doing a lot of front-end work lately.

I am trying to post an array to a server, using jQuery:

$('#fbshare').click(function () {<br></br> $.post('url',<br></br> { array: [ 'only one item' ] },<br></br> function() {<br></br> alert('done!');<br></br> }, 'json');<br></br> });

It turns out that %5b%5d is URL encoding for []. So jQuery “helpfully” making the arrays PHP-style. I don’t need, or want that, but according to this post on the jQuery forum, you can disable that functionality by setting jQuery.ajaxSettings.traditional = true;. I saw something else about disabling it will mean you can’t do deep nested arrays or some such, but I’ll cross that bridge when I come to it. When I post something, I want the name to be the same, not with square brackets after it!