jQuery 1.4 的新行为是一个糟糕的设计选择吗?
这是一个有点咆哮的问题,但也是一个非常严肃的问题。 jQuery 更改了 ajax 参数序列化,如下所示:
jQuery 1.4 在 jQuery.param 中添加了对嵌套参数序列化的支持,使用由 PHP 普及并由 Ruby on Rails 支持的方法。例如,{foo: ["bar", "baz"]} 将被序列化为“foo[]=bar&foo[]=baz”。
你明白了吗?
您将参数命名为 foo
。如果 foo 的值是一个数组,jQuery 现在会在背后将其重命名为 foo[]
。原因是一些 PHP 爱好者和 Ruby 爱好者希望第 3 方 API 为他们重命名。
你可以说我很老套,但是当我使用 x
键将某些内容放入地图时,我希望找到 x
下的值。或者至少将此作为默认行为并具有可选覆盖。
甚至文档也同意我的观点:
如果值是一个数组,jQuery 使用相同的值序列化多个值 键即 {foo:["bar1", "bar2"]} 变为“&foo=bar1&foo=bar2”。
我是否认为这只是 jQuery 团队的错误判断?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它实际上填补了一个主要的不一致,如果您的解串器了解该约定并能很好地使用它。它使单一事物的数组看起来与单独的事物不同。
旧版:
foo: "bar"
映射到"foo=bar"
映射到foo: "bar"
。foo: ["bar"]
映射到"foo=bar"
映射到foo: "bar"
。foo: ["bar", "baz"]
映射到"foo=bar&foo=baz"
映射到foo: ["bar", "baz" “]
。新功能:
foo: "bar"
映射到"foo=bar"
映射到foo: "bar"
。foo: ["bar"]
映射到"foo[]=bar"
映射到foo: ["bar"]
。foo: ["bar", "baz"]
映射到"foo[]=bar&foo[]=baz"
映射到foo: ["bar ”,“巴兹”]
。现在,一切都可以很好地往返,您不必担心接收数组数据或非数组数据,具体取决于数组中开始有多少元素。为了最大程度地优雅,
foo: []
还应该序列化为foo[]
(一个没有值的键),表示一个 0 元列表,但 jQuery 1.4 不这样做这样做。也许应该如此。 :)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 tofoo: "bar"
.foo: ["bar"]
maps to"foo=bar"
maps tofoo: "bar"
.foo: ["bar", "baz"]
maps to"foo=bar&foo=baz"
maps tofoo: ["bar", "baz"]
.New:
foo: "bar"
maps to"foo=bar"
maps tofoo: "bar"
.foo: ["bar"]
maps to"foo[]=bar"
maps tofoo: ["bar"]
.foo: ["bar", "baz"]
maps to"foo[]=bar&foo[]=baz"
maps tofoo: ["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 tofoo[]
(a key with no value), indicating a 0-ary list, but jQuery 1.4 doesn't do that. Maybe it should. :)我不同意。旧的约定工作得很好,并且对于像 Perl 的
CGI.pm
这样的经典 Web 主力来说仍然工作得很好。我认为 jQuery 只是将一种工作惯例转换为另一种工作惯例,而且我不知道双方是否会感到高兴。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.