jQuery 1.4 的新行为是一个糟糕的设计选择吗?

发布于 2024-08-20 10:13:51 字数 657 浏览 3 评论 0原文

这是一个有点咆哮的问题,但也是一个非常严肃的问题。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靑春怀旧 2024-08-27 10:13:51

它实际上填补了一个主要的不一致,如果您的解串器了解该约定并能很好地使用它。它使单一事物的数组看起来与单独的事物不同。

旧版:

  • 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 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. :)

且行且努力 2024-08-27 10:13:51

我不同意。旧的约定工作得很好,并且对于像 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文