1

Given the code below:

requestRefresh("arg1", "arg2");

function requestRefresh ( listofnames ) {
        var args = {
                name:   [],
                action: "ajax.refresh"
        };
        for (var i = 0; i < arguments.length; ++i) {
                args.name.push(arguments[i]);           
        }
        jQuery.get(url, args).done(function ( results ) {
            ...
        });
});

The query gets encoded as:

url?name%5B%5D=arg1&name%5B%5D=arg2&action=ajax.refresh

but I want it without the encoded '[]' after each "name":

url?name=arg1&name=arg2&action=ajax.refresh

Is there any way to accomplish this?

John Hascall
  • 9,176
  • 6
  • 48
  • 72

1 Answers1

1

This behavior exists since 1.4. The previous behavior is what you are looking for

See http://api.jquery.com/jquery.param/

// <=1.3.2:
$.param({ a: [ 2, 3, 4 ] }); // "a=2&a=3&a=4"
// >=1.4:
$.param({ a: [ 2, 3, 4 ] }); // "a[]=2&a[]=3&a[]=4"
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • 1
    You can also call `$.ajax` and use the `traditional: true,` option. That way you don't change the setting globally, just for that request. – Barmar Dec 29 '17 at 23:29
  • 1
    @JohnHascall It's the way most scripting languages expect multiple parameters with the same name to be submitted. If you don't use `[]` they only make the last value available to the application. – Barmar Dec 29 '17 at 23:31
  • Yeah, I ended up changing the receiving framework to accept `a[]=2&a[]=3&a[]=4` (it previously accepted only `a=2&a=3&a=4` or `a[0]=2&a[1]=3&a[2]=4`). It seemed far preferable to telling people you need to do some funny business on the jQuery-side. – John Hascall Dec 30 '17 at 01:11
  • @JohnHascall Yes, that format is more popular then just the `a=2&a=3` except in the Java world, I believe https://stackoverflow.com/questions/13592236/parse-a-uri-string-into-name-value-collection/13592324#13592324 – Ruan Mendes Dec 30 '17 at 14:48