Ticket #2950 (closed bug: invalid)
Not supproted multi-dimensional objects on jQuery.ajax options.data
| Reported by: | aiky | Owned by: | |
|---|---|---|---|
| Priority: | major | Milestone: | 1.3 |
| Component: | ajax | Version: | 1.2.5 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
options.data = {
a: 'a',
b: 'b',
c: {
d: 'd',
e: 'e'
}
}
POST:
a a
b b
c [object+Object]
Change History
comment:2 Changed 5 years ago by aiky
I need to receive on server side:
array(
a=>'a',
b=>'b',
c=>array(
d=>'d',
e=>'e'
)
)
as realised in JsHttpRequest
comment:3 Changed 5 years ago by ygirouard
You can do something like this though:
options.data = {
a: 'a',
b: 'b',
c[d]: 'd',
c[e]: 'e'
}
And on the server-side, the POST request would automatically see c as an array of values.
I do this to pass an array of checkboxes to a php script, the name of each input is something like "input_name[some_index]". Then in php, $_REQUESTinput_name? becomes an array where "some_index" becomes the key.
I do agree though that it should accept multi-dimensional objects as well though, it makes it much easier.
comment:4 Changed 5 years ago by aiky
Yes, may to find some ways for emulating multi-dimensional objects. But would be most corretly to use multi-dimensional objects transparent by core level of jQuery as JSON is multi-dimensional data format and much using in AJAX interactions.
comment:5 Changed 5 years ago by aiky
Ok. Next code translating multi-dimensional object to simple list and can be include to jQuery core for transparent using multi-dimensional objects:
var object2list = function(object, list, _key){
if(!list) var list = {};
for(var key in object){
if(!_key) var _key_ = key;
else var _key_ = _key + '[' + key + ']';
if(typeof(object[key]) != 'object') list[_key_] = object[key];
else list = object2list(object[key], list, _key_);
object[key] = null;
}
return list;
}
comment:6 Changed 4 years ago by dmethvin
- Status changed from new to closed
- Resolution set to invalid
http://docs.jquery.com/Ajax/jQuery.ajax#options
Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key i.e. {foo:["bar1", "bar2"]} becomes '&foo=bar1&foo=bar2'.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

What would you expect the result to be ?