开发者

In JQuery 1.4.2 .ajax converts JSON wrong

开发者 https://www.devze.com 2022-12-24 18:09 出处:网络
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:

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:

?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开发者_Python百科 .ajax. Firebug, after the response is received, shows the post was:

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.4 released a change for Nested param serialization. From their site:

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

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

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 });
0

精彩评论

暂无评论...
验证码 换一张
取 消