views:

117

answers:

2

This is a bit of a rant, but also a very serous question. jQuery has changed ajax param serialization as follows:

jQuery 1.4 adds support for nested param serialization in jQuery.param, using the approach popularized by PHP, and supported by Ruby on Rails. For instance, {foo: ["bar", "baz"]} will be serialized as “foo[]=bar&foo[]=baz”.

Did you catch that?

You call your parameter foo. jQuery now renames that to foo[] behind your back if foo's value is an array. The reason for this is because some PHP-ers and Rubyists expect 3rd party APIs to rename things for them.

Call me old fashioned, but when I put something into a map, with key x, I expect to find the value under x. Or at least have this the default behavior with an optional override.

Even the documentation agrees with me:

If value is an Array, jQuery serializes multiple values with same key i.e. {foo:["bar1", "bar2"]} becomes '&foo=bar1&foo=bar2'.

Am I right in thinking this is simply a bad judgment call from the jQuery team?

+7  A: 

It's actually filling in a major inconsistency, if your deserializer is aware of the convention and works with it nicely. It makes an array-of-one-thing look different from a thing-on-its-own.

Old:

  • foo: "bar" maps to "foo=bar" maps to foo: "bar".
  • foo: ["bar"] maps to "foo=bar" maps to foo: "bar".
  • foo: ["bar", "baz"] maps to "foo=bar&foo=baz" maps to foo: ["bar", "baz"].

New:

  • foo: "bar" maps to "foo=bar" maps to foo: "bar".
  • foo: ["bar"] maps to "foo[]=bar" maps to foo: ["bar"].
  • foo: ["bar", "baz"] maps to "foo[]=bar&foo[]=baz" maps to foo: ["bar", "baz"].

And now everything roundtrips nicely and you don't have to worry about receiving array data or non-array data depending on how many elements were in the array to begin with. For maximum elegance, foo: [] should also serialize to foo[] (a key with no value), indicating a 0-ary list, but jQuery 1.4 doesn't do that. Maybe it should. :)

hobbs
OK, this is a *really* good point. And despite a vote to close this question I've actually learned something here that others can hopefully take away.
z5h
+1  A: 

I disagree. The old convention worked just fine, and continues to work fine for classic web workhorses like Perl's CGI.pm. I think jQuery is just switching one working convention for another, and I don't know that anybody on either side is going to be all that happy.

Randal Schwartz
surely the people on the side jQuery is switching to (PHP and Ruby developers, for starters) will be happy?
nickf
I've loved Perl for long enough to know that not only is CGI.pm's design *frequently* lacking, `param` is one of the worst parts. :)
hobbs