Skip to content

Commit f91b944

Browse files
committed
Objects with length properties weren't getting serialized properly by jQuery.param(). Fixes #5862.
1 parent 76236a1 commit f91b944

File tree

2 files changed

+54
-40
lines changed

2 files changed

+54
-40
lines changed

src/ajax.js

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -603,20 +603,13 @@ jQuery.extend({
603603
// Serialize an array of form elements or a set of
604604
// key/values into a query string
605605
param: function( a, traditional ) {
606-
607606
var s = [];
608607

609608
// Set traditional to true for jQuery <= 1.3.2 behavior.
610609
if ( traditional === undefined ) {
611610
traditional = jQuery.ajaxSettings.traditional;
612611
}
613612

614-
function add( key, value ) {
615-
// If value is a function, invoke it and return its value
616-
value = jQuery.isFunction(value) ? value() : value;
617-
s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
618-
}
619-
620613
// If an array was passed in, assume that it is an array of form elements.
621614
if ( jQuery.isArray(a) || a.jquery ) {
622615
// Serialize the form elements
@@ -627,41 +620,49 @@ jQuery.extend({
627620
} else {
628621
// If traditional, encode the "old" way (the way 1.3.2 or older
629622
// did it), otherwise encode params recursively.
630-
jQuery.each( a, function buildParams( prefix, obj ) {
631-
632-
if ( jQuery.isArray(obj) ) {
633-
// Serialize array item.
634-
jQuery.each( obj, function( i, v ) {
635-
if ( traditional ) {
636-
// Treat each array item as a scalar.
637-
add( prefix, v );
638-
} else {
639-
// If array item is non-scalar (array or object), encode its
640-
// numeric index to resolve deserialization ambiguity issues.
641-
// Note that rack (as of 1.0.0) can't currently deserialize
642-
// nested arrays properly, and attempting to do so may cause
643-
// a server error. Possible fixes are to modify rack's
644-
// deserialization algorithm or to provide an option or flag
645-
// to force array serialization to be shallow.
646-
buildParams( prefix + "[" + ( typeof v === "object" || jQuery.isArray(v) ? i : "" ) + "]", v );
647-
}
648-
});
649-
650-
} else if ( !traditional && obj != null && typeof obj === "object" ) {
651-
// Serialize object item.
652-
jQuery.each( obj, function( k, v ) {
653-
buildParams( prefix + "[" + k + "]", v );
654-
});
655-
656-
} else {
657-
// Serialize scalar item.
658-
add( prefix, obj );
659-
}
660-
});
623+
for ( var prefix in a ) {
624+
buildParams( prefix, a[prefix] );
625+
}
661626
}
662-
627+
663628
// Return the resulting serialization
664629
return s.join("&").replace(r20, "+");
665-
}
666630

631+
function buildParams( prefix, obj ) {
632+
if ( jQuery.isArray(obj) ) {
633+
// Serialize array item.
634+
jQuery.each( obj, function( i, v ) {
635+
if ( traditional ) {
636+
// Treat each array item as a scalar.
637+
add( prefix, v );
638+
} else {
639+
// If array item is non-scalar (array or object), encode its
640+
// numeric index to resolve deserialization ambiguity issues.
641+
// Note that rack (as of 1.0.0) can't currently deserialize
642+
// nested arrays properly, and attempting to do so may cause
643+
// a server error. Possible fixes are to modify rack's
644+
// deserialization algorithm or to provide an option or flag
645+
// to force array serialization to be shallow.
646+
buildParams( prefix + "[" + ( typeof v === "object" || jQuery.isArray(v) ? i : "" ) + "]", v );
647+
}
648+
});
649+
650+
} else if ( !traditional && obj != null && typeof obj === "object" ) {
651+
// Serialize object item.
652+
jQuery.each( obj, function( k, v ) {
653+
buildParams( prefix + "[" + k + "]", v );
654+
});
655+
656+
} else {
657+
// Serialize scalar item.
658+
add( prefix, obj );
659+
}
660+
}
661+
662+
function add( key, value ) {
663+
// If value is a function, invoke it and return its value
664+
value = jQuery.isFunction(value) ? value() : value;
665+
s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
666+
}
667+
}
667668
});

test/unit/ajax.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,19 @@ test("jQuery.getJSON(String, Function) - JSON object with absolute url to local
979979
});
980980
});
981981

982+
test("jQuery.post - data", function() {
983+
expect(2);
984+
stop();
985+
986+
jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
987+
jQuery('math', xml).each(function() {
988+
equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
989+
equals( jQuery('result', this).text(), '3', 'Check for XML' );
990+
});
991+
start();
992+
});
993+
});
994+
982995
test("jQuery.post(String, Hash, Function) - simple with xml", function() {
983996
expect(4);
984997
stop();

0 commit comments

Comments
 (0)