以 json 形式发送数据时 jQuery 表单数据中的括号问题
我拥有该对象
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您还可以在 ajax 调用中使用
传统
设置http://api.jquery.com/jquery.ajax/#jQuery-ajax -设置
例如:
you can also use the
traditional
settings in the ajax callhttp://api.jquery.com/jquery.ajax/#jQuery-ajax-settings
for example:
键中带有括号的这种表示法是在 jQuery 1.4 中引入的,用于处理多维数组或包含对象(或其他数组)本身的数组。这有助于解串器区分数组和原始值。例如,如果键中没有括号,则这两个变量将以相同的方式序列化:
and
使用括号表示法,它们分别编码为
和
。
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:
and
With the bracket notation, they're encoded as
and
respectively.
可以将具有相同键名的多条数据发送到脚本。您可以通过在键名末尾添加方括号
[]
来指定数据应被解释为数组来完成此操作。执行此操作的函数是
jQuery.param
。作为其工作原理的示例: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:data
is an array. When it is serialized, it is rendered asdata%5B%5D=value3&data%5B%5D=value4
. The serverside script will convert this into an array.这主要是一种命名约定(我认为来自 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/