在JQuery 1.4.2 .ajax中,JSON转换错误

时间:2022-08-23 08:10:34

My code worked fine in JQuery 1.3.2, but in 1.4.2 it seems to be broken. What it should get in the post is something like this:

我的代码在JQuery 1.3.2中运行良好,但在1.4.2中,它似乎被破坏了。这篇文章应该是这样的:

?pks=108;pks=107

What I now get is:

我现在得到的是:

?pks[]=108;pks[]=107;

When I trace this code through, the JSON object seems to be fine until it enters .ajax. Firebug, after the response is received, shows the post was:

当我跟踪这段代码时,JSON对象似乎还不错,直到它进入。ajax。收到回复后,Firebug显示:

Parameters  application/x-www-form-urlencoded
pks[]   108
pks[]   107
Source
pks%5B%5D=108&pks%5B%5D=107

Which is not what I got on JQuery 1.3.2. Where are those extra braces coming from?

这不是我在JQuery 1.3.2上得到的。那些额外的牙套是从哪里来的?

1 个解决方案

#1


8  

JQuery 1.4 released a change for Nested param serialization. From their site:

JQuery 1.4为嵌套的param序列化发布了一个更改。从他们的网站:

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

jQuery 1.4增加了对jQuery中嵌套param序列化的支持。使用PHP推广的方法和Ruby on Rails支持的param。例如,{foo:[“bar”,“baz”}将被序列化为“foo[]=bar&foo[]=baz”。

In jQuery 1.3, {foo: ["bar", "baz"]} was serialized as “foo=bar&foo=baz”. However, there was no way to encode a single-element Array using this approach. If you need the old behavior, you can turn it back on by setting the traditional Ajax setting (globally via jQuery.ajaxSettings.traditional or on a case-by-case basis via the traditional flag).

在jQuery 1.3中,{foo: ["bar", "baz"}被序列化为" foo=bar&foo=baz "。但是,没有办法使用这种方法对单元素数组进行编码。如果您需要旧的行为,可以通过设置传统的Ajax设置(全局通过jQuery.ajaxSettings进行设置)来打开它。传统的或基于个案的基础上通过传统的旗帜)。

The [] brackets are normally used to indicate an array, and this appears to be what they are trying to make more obvious here I think (as your query-string could be read to see that one value is being overwritten by the other).

[]方括号通常用于表示数组,我认为这似乎是他们在这里试图使之更明显的地方(正如您的查询字符串可以被读取以看到一个值被另一个值覆盖)。

Edit: I think you could probably follow their suggestion to use either:

编辑:我认为你可以按照他们的建议来使用:

// Globally set it to use the old 1.3.* way of doing things.
jQuery.ajaxSettings.traditional = true;

// Enables the 1.3.* way for a single Ajax request only
$.ajax({ data: stuff, traditional: true });

#1


8  

JQuery 1.4 released a change for Nested param serialization. From their site:

JQuery 1.4为嵌套的param序列化发布了一个更改。从他们的网站:

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

jQuery 1.4增加了对jQuery中嵌套param序列化的支持。使用PHP推广的方法和Ruby on Rails支持的param。例如,{foo:[“bar”,“baz”}将被序列化为“foo[]=bar&foo[]=baz”。

In jQuery 1.3, {foo: ["bar", "baz"]} was serialized as “foo=bar&foo=baz”. However, there was no way to encode a single-element Array using this approach. If you need the old behavior, you can turn it back on by setting the traditional Ajax setting (globally via jQuery.ajaxSettings.traditional or on a case-by-case basis via the traditional flag).

在jQuery 1.3中,{foo: ["bar", "baz"}被序列化为" foo=bar&foo=baz "。但是,没有办法使用这种方法对单元素数组进行编码。如果您需要旧的行为,可以通过设置传统的Ajax设置(全局通过jQuery.ajaxSettings进行设置)来打开它。传统的或基于个案的基础上通过传统的旗帜)。

The [] brackets are normally used to indicate an array, and this appears to be what they are trying to make more obvious here I think (as your query-string could be read to see that one value is being overwritten by the other).

[]方括号通常用于表示数组,我认为这似乎是他们在这里试图使之更明显的地方(正如您的查询字符串可以被读取以看到一个值被另一个值覆盖)。

Edit: I think you could probably follow their suggestion to use either:

编辑:我认为你可以按照他们的建议来使用:

// Globally set it to use the old 1.3.* way of doing things.
jQuery.ajaxSettings.traditional = true;

// Enables the 1.3.* way for a single Ajax request only
$.ajax({ data: stuff, traditional: true });