1 min read

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](http://forum.jquery.com/topic/jquery-post-1-4-1-is-appending-to-vars-when-posting-from-array-within-array#14737000000691277), you can disable that functionality by setting <tt>jQuery.ajaxSettings.traditional = true;</tt>. 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!