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

Commit 3951894

Browse files
cowboyjeresig
authored andcommitted
Moved jQuery.param "traditional" flag into jQuery.ajaxSettings, can now be overridden via 2nd argument to jQuery.param
1 parent 7d0c180 commit 3951894

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/ajax.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jQuery.fn.extend({
4040

4141
// Otherwise, build a param string
4242
} else if ( typeof params === "object" ) {
43-
params = jQuery.param( params );
43+
params = jQuery.param( params, jQuery.ajaxSettings.traditional );
4444
type = "POST";
4545
}
4646
}
@@ -172,6 +172,7 @@ jQuery.extend({
172172
data: null,
173173
username: null,
174174
password: null,
175+
traditional: false,
175176
*/
176177
// Create the request object; Microsoft failed to properly
177178
// implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
@@ -204,7 +205,7 @@ jQuery.extend({
204205

205206
// convert data if not already a string
206207
if ( s.data && s.processData && typeof s.data !== "string" ) {
207-
s.data = jQuery.param(s.data);
208+
s.data = jQuery.param( s.data, s.traditional );
208209
}
209210

210211
// Handle JSONP Parameter Callbacks
@@ -594,12 +595,14 @@ jQuery.extend({
594595

595596
// Serialize an array of form elements or a set of
596597
// key/values into a query string
597-
param: function( a ) {
598+
param: function( a, traditional ) {
598599

599-
var s = [],
600-
601-
// Set jQuery.param.traditional to true for jQuery <= 1.3.2 behavior.
602-
traditional = jQuery.param.traditional;
600+
var s = [];
601+
602+
// Set traditional to true for jQuery <= 1.3.2 behavior.
603+
if ( traditional === undefined ) {
604+
traditional = jQuery.ajaxSettings.traditional;
605+
}
603606

604607
function add( key, value ) {
605608
// If value is a function, invoke it and return its value
@@ -615,8 +618,8 @@ jQuery.extend({
615618
});
616619

617620
} else {
618-
// If jQuery.param.traditional is true, encode the "old" way (the
619-
// way 1.3.2 or older did it), otherwise encode params recursively.
621+
// If traditional, encode the "old" way (the way 1.3.2 or older
622+
// did it), otherwise encode params recursively.
620623
jQuery.each( a, function buildParams( prefix, obj ) {
621624

622625
if ( jQuery.isArray(obj) ) {

test/unit/ajax.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ test("serialize()", function() {
258258
});
259259

260260
test("jQuery.param()", function() {
261-
expect(15);
261+
expect(17);
262262

263-
equals( jQuery.param.traditional, undefined, "traditional flag, undefined by default" );
263+
equals( !jQuery.ajaxSettings.traditional, true, "traditional flag, falsy by default" );
264264

265265
var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
266266
equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
@@ -283,7 +283,10 @@ test("jQuery.param()", function() {
283283
params = { a: [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { b: [ 7, [ 8, 9 ], [ { c: 10, d: 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { e: { f: { g: [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] };
284284
equals( decodeURIComponent( jQuery.param(params) ), "a[]=0&a[1][]=1&a[1][]=2&a[2][]=3&a[2][1][]=4&a[2][1][]=5&a[2][2][]=6&a[3][b][]=7&a[3][b][1][]=8&a[3][b][1][]=9&a[3][b][2][0][c]=10&a[3][b][2][0][d]=11&a[3][b][3][0][]=12&a[3][b][4][0][0][]=13&a[3][b][5][e][f][g][]=14&a[3][b][5][e][f][g][1][]=15&a[3][b][]=16&a[]=17", "nested arrays" );
285285

286-
jQuery.param.traditional = true;
286+
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?" };
287+
equals( jQuery.param(params,true), "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, forced traditional" );
288+
289+
jQuery.ajaxSetup({ traditional: true });
287290

288291
var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
289292
equals( jQuery.param(params), "foo=bar&baz=42&quux=All+your+base+are+belong+to+us", "simple" );
@@ -305,6 +308,10 @@ test("jQuery.param()", function() {
305308

306309
params = { a: [ 0, [ 1, 2 ], [ 3, [ 4, 5 ], [ 6 ] ], { b: [ 7, [ 8, 9 ], [ { c: 10, d: 11 } ], [ [ 12 ] ], [ [ [ 13 ] ] ], { e: { f: { g: [ 14, [ 15 ] ] } } }, 16 ] }, 17 ] };
307310
equals( jQuery.param(params), "a=0&a=1%2C2&a=3%2C4%2C5%2C6&a=%5Bobject+Object%5D&a=17", "nested arrays (not possible when jQuery.param.traditional == true)" );
311+
312+
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?" };
313+
equals( decodeURIComponent( jQuery.param(params,false) ), "a[]=1&a[]=2&b[c]=3&b[d][]=4&b[d][]=5&b[e][x][]=6&b[e][y]=7&b[e][z][]=8&b[e][z][]=9&b[f]=true&b[g]=false&b[h]=undefined&i[]=10&i[]=11&j=true&k=false&l[]=undefined&l[]=0&m=cowboy+hat?", "huge structure, forced not traditional" );
314+
308315

309316
});
310317

0 commit comments

Comments
 (0)