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

Commit 67089ee

Browse files
committed
Getting $.param working well; Patch by ben_alman
1 parent 45dfa3b commit 67089ee

File tree

2 files changed

+64
-34
lines changed

2 files changed

+64
-34
lines changed

src/ajax.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -585,39 +585,52 @@ jQuery.extend({
585585
// Serialize an array of form elements or a set of
586586
// key/values into a query string
587587
param: function( a ) {
588-
var s = [];
589-
588+
var s = [],
589+
param_traditional = jQuery.param.traditional;
590+
590591
function add( key, value ){
592+
// If value is a function, invoke it and return its value
593+
value = jQuery.isFunction(value) ? value() : value;
591594
s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
592595
}
593-
596+
594597
// If an array was passed in, assume that it is an array
595598
// of form elements
596-
if ( jQuery.isArray(a) || a.jquery ) {
599+
if ( jQuery.isArray(a) || a.jquery )
597600
// Serialize the form elements
598601
jQuery.each( a, function() {
599602
add( this.name, this.value );
600603
});
601-
} else {
602-
// Recursively encode parameters from object,
603-
// building a prefix path as we go down
604-
function buildParams(obj, prefix)
605-
{
606-
if ( jQuery.isArray(obj) ) {
607-
for ( var i = 0, length = obj.length; i < length; i++ ) {
608-
buildParams( obj[i], prefix );
609-
};
610-
} else if( typeof(obj) == "object" ) {
611-
for ( var j in obj ) {
612-
var postfix = ((j.indexOf("[]") > 0) ? "[]" : ""); // move any brackets to the end
613-
buildParams(obj[j], (prefix ? (prefix+"["+j.replace("[]", "")+"]"+postfix) : j) );
614-
}
615-
} else {
616-
add( prefix, jQuery.isFunction(obj) ? obj() : obj );
617-
}
618-
}
619-
buildParams(a);
620-
}
604+
605+
else
606+
// Encode parameters from object, recursively. If
607+
// jQuery.param.traditional is set, encode the "old" way
608+
// (the way 1.3.2 or older did it)
609+
jQuery.each( a, function buildParams( prefix, obj ) {
610+
611+
if ( jQuery.isArray(obj) )
612+
jQuery.each( obj, function(i,v){
613+
// Due to rails' limited request param syntax, numeric array
614+
// indices are not supported. To avoid serialization ambiguity
615+
// issues, serialized arrays can only contain scalar values. php
616+
// does not have this issue, but we should go with the lowest
617+
// common denominator
618+
add( prefix + ( param_traditional ? "" : "[]" ), v );
619+
});
620+
621+
else if ( typeof obj == "object" )
622+
if ( param_traditional )
623+
add( prefix, obj );
624+
625+
else
626+
jQuery.each( obj, function(k,v){
627+
buildParams( prefix ? prefix + "[" + k + "]" : k, v );
628+
});
629+
630+
else
631+
add( prefix, obj );
632+
633+
});
621634

622635
// Return the resulting serialization
623636
return s.join("&").replace(r20, "+");

test/unit/ajax.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,30 @@ test("serialize()", function() {
251251
});
252252

253253
test("jQuery.param()", function() {
254-
expect(8);
254+
expect(13);
255+
256+
equals( jQuery.param.traditional, undefined, "traditional flag, undefined by default" );
257+
258+
var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
259+
equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
260+
261+
params = {someName: [1, 2, 3], regularThing: "blah" };
262+
equals( jQuery.param(params), "someName%5B%5D=1&someName%5B%5D=2&someName%5B%5D=3&regularThing=blah", "with array" );
263+
264+
params = {foo: ['a', 'b', 'c']};
265+
equals( jQuery.param(params), "foo%5B%5D=a&foo%5B%5D=b&foo%5B%5D=c", "with array of strings" );
266+
267+
params = {foo: ["baz", 42, "All your base are belong to us"] };
268+
equals( jQuery.param(params), "foo%5B%5D=baz&foo%5B%5D=42&foo%5B%5D=All+your+base+are+belong+to+us", "more array" );
269+
270+
params = {foo: { bar: 'baz', beep: 42, quux: 'All your base are belong to us' } };
271+
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" );
272+
273+
params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
274+
equals( jQuery.param(params), "a%5B%5D=1&a%5B%5D=2&b%5Bc%5D=3&b%5Bd%5D%5B%5D=4&b%5Bd%5D%5B%5D=5&b%5Be%5D%5Bx%5D%5B%5D=6&b%5Be%5D%5By%5D=7&b%5Be%5D%5Bz%5D%5B%5D=8&b%5Be%5D%5Bz%5D%5B%5D=9&b%5Bf%5D=true&b%5Bg%5D=false&b%5Bh%5D=undefined&i%5B%5D=10&i%5B%5D=11&j=true&k=false&l%5B%5D=undefined&l%5B%5D=0&m=cowboy+hat%3F", "huge structure" );
275+
276+
jQuery.param.traditional = true;
277+
255278
var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
256279
equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
257280

@@ -266,15 +289,9 @@ test("jQuery.param()", function() {
266289

267290
params = {"foo[bar]":"baz", "foo[beep]":42, "foo[quux]":"All your base are belong to us"};
268291
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" );
269-
270-
params = {foo: {bar: "baz", beep: 42, quux: "All your base are belong to us"}};
271-
equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bquux%5D=All+your+base+are+belong+to+us", "nested object" );
272-
273-
params = {foo: {"bar[]": [1,2,3]}};
274-
equals( jQuery.param(params), "foo%5Bbar%5D%5B%5D=1&foo%5Bbar%5D%5B%5D=2&foo%5Bbar%5D%5B%5D=3", "nested array");
275-
276-
params = {foo: [{bar: "baz", beep: 42}, {bar: "baz2", beep: 43}]};
277-
equals( jQuery.param(params), "foo%5Bbar%5D=baz&foo%5Bbeep%5D=42&foo%5Bbar%5D=baz2&foo%5Bbeep%5D=43", "nested array of objects" );
292+
293+
params = { a:[1,2], b:{ c:3, d:[4,5], e:{ x:[6], y:7, z:[8,9] }, f:true, g:false, h:undefined }, i:[10,11], j:true, k:false, l:[undefined,0], m:"cowboy hat?" };
294+
equals( jQuery.param(params), "a=1&a=2&b=%5Bobject+Object%5D&i=10&i=11&j=true&k=false&l=undefined&l=0&m=cowboy+hat%3F", "huge structure" );
278295

279296
});
280297

0 commit comments

Comments
 (0)