#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 )
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)
Change History (13)
comment:1 Changed 13 years ago by
Description: | modified (diff) |
---|
Changed 13 years ago by
Attachment: | test-6900.html added |
---|
comment:2 Changed 13 years ago by
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
Milestone: | 1.4.2 → 1.5 |
---|---|
Priority: | → low |
Status: | new → open |
Type: | bug → 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 12 years ago by
Keywords: | ajaxrewrite added |
---|
comment:5 Changed 12 years ago by
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:
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
Owner: | set to benalman |
---|---|
Status: | open → assigned |
Type: | enhancement → bug |
Not sure if this is an issue - Ben, any thoughts?
comment:7 Changed 12 years ago by
Version: | 1.4.2 → 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 12 years ago by
Milestone: | → 1.next |
---|
comment:9 Changed 12 years ago by
Owner: | changed from benalman to cowboy |
---|
comment:10 Changed 12 years ago by
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
Keywords: | needsdocs added |
---|---|
Resolution: | → cantfix |
Status: | assigned → 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.
Data sent by the test case looked like this in Firebug: