以 json 形式发送数据时 jQuery 表单数据中的括号问题

发布于 2024-11-07 13:19:10 字数 729 浏览 8 评论 0原文

我拥有该对象

    var dataformdata={"key1":"value1","key2":"value2"};

,然后我使用相同的键(key3)添加更多值,就像

    dataformdata.key3 = [];
    dataformdata.key3.push("value3");
    dataformdata.key3.push("value4");

我在每个斜率中执行上述操作一样。除了通过浏览器控制台中的 jQuery ajax 函数发送 dataformdata 对象时,这一切都有效,我看到键中有括号...

$.ajax({ 类型:“帖子”, 网址:“/”, data: dataformdata,...

这是我在浏览器控制台中看到的:

key1:value1
key2:value2
key3%5B%5D:value3
key3%5B%5D:value4

它应该可以工作,因为在 jQuery.ajax() 文档中它说

对象必须是键/值对。如果value是一个Array,jQuery会根据传统设置的值序列化具有相同key的多个值

但是为什么key中会有括号(%5B%5D)?

I have the object

    var dataformdata={"key1":"value1","key2":"value2"};

then I add some more values with the same key(key3) like this

    dataformdata.key3 = [];
    dataformdata.key3.push("value3");
    dataformdata.key3.push("value4");

I do the above in an each slope. It all works except when sending the dataformdata object via the jQuery ajax function in the browser console I see that there are brackets in the key ...

$.ajax({
type: "POST",
url: "/",
data: dataformdata,
...

This is what I see in the browser console:

key1:value1
key2:value2
key3%5B%5D:value3
key3%5B%5D:value4

It should work because in the jQuery.ajax() docs it says

Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting

But why are the brackets (%5B%5D) in the key?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

酒中人 2024-11-14 13:19:10

您还可以在 ajax 调用中使用传统设置
http://api.jquery.com/jquery.ajax/#jQuery-ajax -设置

传统类型:布尔型

如果您希望使用
参数序列化的传统风格。

例如:

$.ajax({
 /*usual stuff */
 traditional: true
})

you can also use the traditional settings in the ajax call
http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings

traditional Type: Boolean

Set this to true if you wish to use the
traditional style of param serialization.

for example:

$.ajax({
 /*usual stuff */
 traditional: true
})
小伙你站住 2024-11-14 13:19:10

键中带有括号的这种表示法是在 jQuery 1.4 中引入的,用于处理多维数组或包含对象(或其他数组)本身的数组。这有助于解串器区分数组和原始值。例如,如果键中没有括号,则这两个变量将以相同的方式序列化:

var v1 = { "k1":"v1", "k2":"v2", "k3":["v3"] };

and

var v1 = { "k1":"v1", "k2":"v2", "k3":"v3" };

使用括号表示法,它们分别编码为

k1=v2&k2=v2&k3[]=v3

k1=v2&k2=v2&k3=v3

This notation with the brackets in the key was introduced in jQuery 1.4 to deal with multi-dimensional arrays, or arrays containing objects (or other arrays) themselves. This helps the deserializer to differentiate between an array and a primitive value. For example, if you didn't have the brackets in the key, those two variables would be serialized the same way:

var v1 = { "k1":"v1", "k2":"v2", "k3":["v3"] };

and

var v1 = { "k1":"v1", "k2":"v2", "k3":"v3" };

With the bracket notation, they're encoded as

k1=v2&k2=v2&k3[]=v3

and

k1=v2&k2=v2&k3=v3

respectively.

枕花眠 2024-11-14 13:19:10

可以将具有相同键名的多条数据发送到脚本。您可以通过在键名末尾添加方括号 [] 来指定数据应被解释为数组来完成此操作。

执行此操作的函数是 jQuery.param。作为其工作原理的示例:

$.param({
    data: ['value3', 'value4']
});

data 是一个数组。序列化后,将呈现为 data%5B%5D=value3&data%5B%5D=value4。服务器端脚本会将其转换为数组。

It is possible to send multiple pieces of data with the same key name to a script. You can do this by adding square brackets [] to the end of the key name to designate that the data should be interpreted as an array.

The function that does this is jQuery.param. As an example of how this works:

$.param({
    data: ['value3', 'value4']
});

data is an array. When it is serialized, it is rendered as data%5B%5D=value3&data%5B%5D=value4. The serverside script will convert this into an array.

蓦然回首 2024-11-14 13:19:10

这主要是一种命名约定(我认为来自 PHP),表明键 (key3) 是多值的。由服务器来对这些进行有意义的解码。

更多详细信息: http://api.jquery.com/jQuery.param/

This is mostly a naming convention — I think from PHP — that indicates that the key (key3) is multivalued. It's up to the server to decode these meaningfully.

More details: http://api.jquery.com/jQuery.param/

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