Skip to main content

Bug Tracker

Side navigation

Ticket #2726: param.diff


File param.diff, 1.7 KB (added by flesler, May 14, 2008 10:12PM UTC)

This is the change, but I think I won't be able to apply it as many people/servers don't expect this to happen.

Index: src/ajax.js
===================================================================
--- src/ajax.js	(revision 5600)
+++ src/ajax.js	(working copy)
@@ -479,25 +479,39 @@
 	param: function( a ) {
 		var s = [];
 
+		function add( key, value ){
+			s[s.length] = key + "=" + encodeURIComponent( value );
+		};
+
 		// If an array was passed in, assume that it is an array
 		// of form elements
 		if ( a.constructor == Array || a.jquery )
 			// Serialize the form elements
 			jQuery.each( a, function(){
-				s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
+				add( encodeURIComponent(this.name), this.value );
 			});
 
 		// Otherwise, assume that it's an object of key/value pairs
 		else
 			// Serialize the key/values
-			for ( var j in a )
-				// If the value is an array then the key names need to be repeated
-				if ( a[j] && a[j].constructor == Array )
-					jQuery.each( a[j], function(){
-						s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
+			for ( var j in a ){
+				var obj = a[j];
+				
+				// Encode it, but don't encode the squared brackets afterwards
+				j = encodeURIComponent(j);
+				
+				// If the value is an array or a map, generate name[key]=value
+				if ( obj && typeof obj == 'object' )
+					jQuery.each( obj, function( key, v ){
+						var name = j;
+						// Don't break name[]=v1&name[]=v2 ...
+						if( j.indexOf('[]') == -1 )
+							name += '['+k+']';
+						add( name , v );
 					});
 				else
-					s.push( encodeURIComponent(j) + "=" + encodeURIComponent( jQuery.isFunction(a[j]) ? a[j]() : a[j] ) );
+					add( j, jQuery.isFunction(obj) ? obj() : obj );
+			}
 
 		// Return the resulting serialization
 		return s.join("&").replace(/%20/g, "+");

Download in other formats:

Original Format