Bug Tracker

Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#6900 closed bug (cantfix)

parsing of arrays in the function "buildParams"

Reported by: adescalz Owned by: cowboy
Priority: low Milestone: 1.next
Component: ajax Version: 1.6b1
Keywords: parsing, array, ajaxrewrite Cc:
Blocked by: Blocking:

Description (last modified by dmethvin)

based on this question in stackoverflow: "http://stackoverflow.com/questions/3462087/passing-an-nested-arrays-to-asp-net-mvc-using-jquerys-ajax"

I look as parse arrays, and I find the function "buildParams" in the method "param".

in line 5290:

buildParams( prefix + "[" + ( typeof v === "object" || 
jQuery.isArray(v) ? i : "" ) + "]", v );

I change this code:

buildParams(prefix + (typeof v === "object" || 
jQuery.isArray(v) ? "[" 
+ i + "]" : ""), v);

and the result was good. I could not test on many more cases.

I hope it's useful.

greetings.

Andres.

Attachments (1)

test-6900.html (672 bytes) - added by dmethvin 13 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 13 years ago by dmethvin

Description: modified (diff)

Data sent by the test case looked like this in Firebug:

subgroups[0][0][]	A
subgroups[0][0][]	Y
subgroups[0][1][]	B
subgroups[0][1][]	Y
subgroups[0][2][]	C
subgroups[0][2][]	Y
subgroups[1][0][]	D
subgroups[1][0][]	Z
subgroups[1][1][]	E
subgroups[1][1][]	Z
subgroups[1][2][]	F
subgroups[1][2][]	Z

Changed 13 years ago by dmethvin

Attachment: test-6900.html added

comment:2 Changed 13 years ago by jeffvanb

I am the original poster from stackoverflow. I attempted the change the adescalz recommended but could not find the corresponding line in jQuery-1.4.2.

Instead I changed line 5419 to:

buildParams( prefix + "[" + i + "]", v );

Not sure how this method works with PHP and Ruby, etc. But this formatting fixes the problem when passing nested arrays to asp.net mvc.

comment:3 Changed 12 years ago by SlexAxton

Milestone: 1.4.21.5
Priority: low
Status: newopen
Type: bugenhancement

Marking as an enhancement. We need to make sure this is something that all communities would benefit from. We got some pretty negative feedback on changing the param stuff to support a few languages in 1.4.2, and not others. Anyone want to weigh in? Is this directly related to that change?

Marking this as an enhancement, regardless.

comment:4 Changed 12 years ago by Rick Waldron

Keywords: ajaxrewrite added

comment:5 Changed 12 years ago by [email protected]

I actually consider this a bug.

http://jsfiddle.net/5PTY9/

An object with an array of objects as one of its properties should not serialize out this way.

The bug is in buildParams, in the portion where it serializes an object - that portion of the else statement is doubly broken - it both places [ ] around object property names instead of passing a serialized object, and in other scenarios completely loses data by passing empty arrays like list[] when list is an object with basic properties like strings.

That said, it would appear the solution is to use JSON.stringify from json2 on all data passed to jquery, rather than to fix this code, considering the author of JQuery is begging coders to do so:

http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/

It may be best to just eliminate the broken code from jquery entirely and throw an exception telling the programmer to use json2.js: https://github.com/douglascrockford/JSON-js

comment:6 Changed 12 years ago by john

Owner: set to benalman
Status: openassigned
Type: enhancementbug

Not sure if this is an issue - Ben, any thoughts?

comment:7 Changed 12 years ago by timmywil

Version: 1.4.21.6b1

I think param does the correct behavior. Data is not lost, but it does not automatically go to JSON form because that is not the default for decoding arrays to uri. If you want JSON, encode to JSON before passing to param. We could theoretically add JSON encoding to jQuery and provide param with an argument on whether it should decode into stringified JSON, but I don't think it should be the default.

comment:8 Changed 12 years ago by timmywil

Milestone: 1.next

comment:9 Changed 12 years ago by Rick Waldron

Owner: changed from benalman to cowboy

comment:10 Changed 12 years ago by cowboy

Unfortunately, there is no universally agreed-upon spec or RFC for "params strings" and as such, it's virtually impossible to encode very complicated nested data structures in a way that works across all frameworks or languages that support these "modern" params strings.

The current jQuery $.param implementation has been tested and is working (with minor, documented, exceptions) in both PHP and Ruby on Rails environments, and I'd be hesitant to make additional changes.

Perhaps a two-fold approach of a) documenting this issue and b) recommending that JSON be used for transmitting more complex data structures (nested arrays, mainly) to the server would be useful.

comment:11 Changed 12 years ago by ajpiano

Keywords: needsdocs added
Resolution: cantfix
Status: assignedclosed

Based on cowboy's feedback above, I am going to close this with 'cantfix' as there is no "right way" to do this, and recommending that we mention his suggestion to use JSON to send arbitrarily nested objects over the wire in the docs.

comment:12 Changed 12 years ago by addyosmani

Keywords: needsdocs removed

Docs updated.

Note: See TracTickets for help on using tickets.