Bug Tracker

Opened 6 years ago

Closed 5 years ago

Last modified 5 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.



Change History (12)

comment:1 Changed 6 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

comment:2 Changed 6 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 6 years ago by SlexAxton

  • Milestone changed from 1.4.2 to 1.5
  • Priority set to low
  • Status changed from new to open
  • Type changed from bug to enhancement

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 5 years ago by rwaldron

  • Keywords ajaxrewrite added

comment:5 Changed 5 years ago by chris@…

I actually consider this a bug.


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:


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 5 years ago by john

  • Owner set to benalman
  • Status changed from open to assigned
  • Type changed from enhancement to bug

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

comment:7 Changed 5 years ago by timmywil

  • Version changed from 1.4.2 to 1.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 5 years ago by timmywil

  • Milestone set to 1.next

comment:9 Changed 5 years ago by rwaldron

  • Owner changed from benalman to cowboy

comment:10 Changed 5 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 5 years ago by ajpiano

  • Keywords needsdocs added
  • Resolution set to cantfix
  • Status changed from assigned to closed

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 5 years ago by addyosmani

  • Keywords needsdocs removed

Docs updated.

Note: See TracTickets for help on using tickets.