发送数据时,括号中的jQuery表单数据作为JSON与问题 [英] Problem with brackets in jQuery Form Data when sending data as json

查看:132
本文介绍了发送数据时,括号中的jQuery表单数据作为JSON与问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对象

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

然后我添加一些更多的价值与像这样相同的密钥(密钥)

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

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

我做了上述的每一个斜坡研究。它通过jQuery的AJAX功能的浏览器控制台发送dataformdata对象时,除了所有的作品我看到有括号中的关键...

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 ...

$。阿贾克斯({             键入:POST,             网址:/,             数据:dataformdata, ...

$.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

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

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

对象必须是键/值对。如果值是一个数组,jQuery的序列化多个值相同的密钥基于传统设置的值

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

但是,为什么是关键中的括号(%5B%5D)?

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

推荐答案

这个符号与括号内键的jQuery 1.4引入处理多维数组,或包含对象(或其他阵列)自身的数组。这有助于解串器的阵列和一基本值之间进行区分。举例来说,如果你没有足够的括号中的关键,这两个变量将被序列化以同样的方式:

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"] };

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

通过支架符号,它们是连接codeD为

With the bracket notation, they're encoded as

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

k1=v2&k2=v2&k3=v3

分别。

这篇关于发送数据时,括号中的jQuery表单数据作为JSON与问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆