Skip to main content

Bug Tracker

Side navigation

#6900 closed bug (cantfix)

Opened August 11, 2010 09:42PM UTC

Closed July 12, 2011 07:16PM UTC

Last modified July 13, 2011 05:18AM UTC

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

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 (0.7 KB) - added by dmethvin August 11, 2010 11:09PM UTC.
Change History (12)

Changed August 11, 2010 11:08PM UTC by dmethvin comment:1

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

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 August 13, 2010 03:02PM UTC by jeffvanb comment:2

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.

Changed October 25, 2010 06:50AM UTC by SlexAxton comment:3

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.

Changed December 27, 2010 10:37PM UTC by rwaldron comment:4

keywords: parsing arrayparsing,array,ajaxrewrite

Changed January 24, 2011 07:21PM UTC by chris@brass9.com comment:5

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

Changed April 16, 2011 08:45PM UTC by john comment:6

owner: → benalman
status: openassigned
type: enhancementbug

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

Changed April 17, 2011 05:36PM UTC by timmywil comment:7

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.

Changed April 17, 2011 05:37PM UTC by timmywil comment:8

milestone: → 1.next

Changed July 12, 2011 06:46PM UTC by rwaldron comment:9

owner: benalmancowboy

Changed July 12, 2011 07:10PM UTC by cowboy comment:10

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.

Changed July 12, 2011 07:16PM UTC by ajpiano comment:11

keywords: parsing,array,ajaxrewriteparsing,array,ajaxrewrite,needsdocs
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.

Changed July 13, 2011 05:18AM UTC by addyosmani comment:12

keywords: parsing,array,ajaxrewrite,needsdocsparsing,array,ajaxrewrite

Docs updated.