Kamilski81 Kamilski81 - 1 year ago 106
jQuery Question

How do I send an array using jQuery and the .ajax method without escaping the brackets?

I am trying to send the same parameter name with multiple values, but even after reading the posts on SO can't figure out how to do it... I want to have an array of


var myObject = {
search_zip: params.search_zip,
search_distance: params.search_distance,
filter_opts: params.filter_opts,
page: params.page,
destination_input: ['323 w concord pl #8, chicago, il', '11 e hubbard, chicago, il']

but this creates a query string of:


As you can see it keeps adding
to the
such as

This means everything gets messed up on the backend. Any ideas?

Answer Source

I'm going under the assumption that your backend is not PHP, since PHP expects [] to indicate that an GET parameter is multivalued. That means that your backend will treat multiple GET arguments with the same key as elements of an array, correct?

Going off that assumption, you just need to tell jQuery to not automatically add [] when it is turning an array into a GET argument string.

To so that, you have to pass the 'traditional': true argument as an option to jQuery's ajax function.

Search 'traditional' on here: http://api.jquery.com/jQuery.ajax/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download