Objects with length properties weren't getting serialized properly by jQuery.param(). Fixes #5862.

This commit is contained in:
John Resig 2010-01-25 00:46:03 -05:00
parent 76236a1506
commit f91b944cab
2 changed files with 54 additions and 40 deletions

View file

@ -603,7 +603,6 @@ jQuery.extend({
// Serialize an array of form elements or a set of
// key/values into a query string
param: function( a, traditional ) {
var s = [];
// Set traditional to true for jQuery <= 1.3.2 behavior.
@ -611,12 +610,6 @@ jQuery.extend({
traditional = jQuery.ajaxSettings.traditional;
}
function add( key, value ) {
// If value is a function, invoke it and return its value
value = jQuery.isFunction(value) ? value() : value;
s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
}
// If an array was passed in, assume that it is an array of form elements.
if ( jQuery.isArray(a) || a.jquery ) {
// Serialize the form elements
@ -627,8 +620,15 @@ jQuery.extend({
} else {
// If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
jQuery.each( a, function buildParams( prefix, obj ) {
for ( var prefix in a ) {
buildParams( prefix, a[prefix] );
}
}
// Return the resulting serialization
return s.join("&").replace(r20, "+");
function buildParams( prefix, obj ) {
if ( jQuery.isArray(obj) ) {
// Serialize array item.
jQuery.each( obj, function( i, v ) {
@ -657,11 +657,12 @@ jQuery.extend({
// Serialize scalar item.
add( prefix, obj );
}
});
}
// Return the resulting serialization
return s.join("&").replace(r20, "+");
function add( key, value ) {
// If value is a function, invoke it and return its value
value = jQuery.isFunction(value) ? value() : value;
s[ s.length ] = encodeURIComponent(key) + "=" + encodeURIComponent(value);
}
}
});

View file

@ -979,6 +979,19 @@ test("jQuery.getJSON(String, Function) - JSON object with absolute url to local
});
});
test("jQuery.post - data", function() {
expect(2);
stop();
jQuery.post(url("data/name.php"), {xml: "5-2", length: 3}, function(xml){
jQuery('math', xml).each(function() {
equals( jQuery('calculation', this).text(), '5-2', 'Check for XML' );
equals( jQuery('result', this).text(), '3', 'Check for XML' );
});
start();
});
});
test("jQuery.post(String, Hash, Function) - simple with xml", function() {
expect(4);
stop();