🌐 AI搜索 & 代理 主页
Skip to content

Commit 50d78e7

Browse files
committed
Adds nested param serialization; Closes #4201 (by merbjedi)
1 parent 569c8b4 commit 50d78e7

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/ajax.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -588,23 +588,28 @@ jQuery.extend({
588588
// of form elements
589589
if ( jQuery.isArray(a) || a.jquery ) {
590590
// Serialize the form elements
591-
jQuery.each( a, function(){
591+
jQuery.each( a, function() {
592592
add( this.name, this.value );
593593
});
594-
595-
// Otherwise, assume that it's an object of key/value pairs
596594
} else {
597-
// Serialize the key/values
598-
for ( var j in a ) {
599-
// If the value is an array then the key names need to be repeated
600-
if ( jQuery.isArray(a[j]) ) {
601-
jQuery.each( a[j], function(){
602-
add( j, this );
603-
});
595+
// Recursively encode parameters from object,
596+
// building a prefix path as we go down
597+
function buildParams(obj, prefix)
598+
{
599+
if ( jQuery.isArray(obj) ) {
600+
for ( var i = 0, length = obj.length; i < length; i++ ) {
601+
buildParams( obj[i], prefix );
602+
};
603+
} else if( typeof(obj) == "object" ) {
604+
for ( var j in obj ) {
605+
var postfix = ((j.indexOf("[]") > 0) ? "[]" : ""); // move any brackets to the end
606+
buildParams(obj[j], (prefix ? (prefix+"["+j.replace("[]", "")+"]"+postfix) : j) );
607+
}
604608
} else {
605-
add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );
609+
add( prefix, jQuery.isFunction(obj) ? obj() : obj );
606610
}
607611
}
612+
buildParams(a);
608613
}
609614

610615
// Return the resulting serialization

test/unit/ajax.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,31 @@ test("serialize()", function() {
213213
});
214214

215215
test("jQuery.param()", function() {
216-
expect(4);
216+
expect(8);
217217
var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
218218
equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
219219

220220
params = {someName: [1, 2, 3], regularThing: "blah" };
221221
equals( jQuery.param(params), "someName=1&someName=2&someName=3&regularThing=blah", "with array" );
222222

223+
params = {foo: ['a', 'b', 'c']};
224+
equals( jQuery.param(params), "foo=a&foo=b&foo=c", "with array of strings" );
225+
223226
params = {"foo[]":["baz", 42, "All your base are belong to us"]};
224227
equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
225228

226229
params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
227230
equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "even more arrays" );
231+
232+
params = {foo: {bar: "baz", beep: 42, quux: "All your base are belong to us"}};
233+
equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "nested object" );
234+
235+
params = {foo: {"bar[]": [1,2,3]}};
236+
equals( jQuery.param(params), "foo%5Bbar%5D%5B%5D=1&foo%5Bbar%5D%5B%5D=2&foo%5Bbar%5D%5B%5D=3", "nested array");
237+
238+
params = {foo: [{bar: "baz", beep: 42}, {bar: "baz2", beep: 43}]};
239+
equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bbar%5D=baz2&foo%5Bbeep%5D=43", "nested array of objects" );
240+
228241
});
229242

230243
test("synchronous request", function() {

0 commit comments

Comments
 (0)