Skip to content
Permalink
Browse files

Moved jQuery.param "traditional" flag into jQuery.ajaxSettings, can n…

…ow be overridden via 2nd argument to jQuery.param
  • Loading branch information
cowboy authored and jeresig committed Dec 22, 2009
1 parent 7d0c180 commit 39518945047413f1185682078043e70e0c5c9091
Showing with 22 additions and 12 deletions.
  1. +12 −9 src/ajax.js
  2. +10 −3 test/unit/ajax.js
@@ -40,7 +40,7 @@ jQuery.fn.extend({

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

// convert data if not already a string
if ( s.data && s.processData && typeof s.data !== "string" ) {
s.data = jQuery.param(s.data);
s.data = jQuery.param( s.data, s.traditional );
}

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

// Serialize an array of form elements or a set of
// key/values into a query string
param: function( a ) {
param: function( a, traditional ) {

var s = [],

// Set jQuery.param.traditional to true for jQuery <= 1.3.2 behavior.
traditional = jQuery.param.traditional;
var s = [];

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

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

} else {
// If jQuery.param.traditional is true, encode the "old" way (the
// way 1.3.2 or older did it), otherwise encode params recursively.
// 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 ) {

if ( jQuery.isArray(obj) ) {
@@ -258,9 +258,9 @@ test("serialize()", function() {
});

test("jQuery.param()", function() {
expect(15);
expect(17);

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

var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
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() {
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 ] };
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" );

jQuery.param.traditional = true;
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?" };
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" );

jQuery.ajaxSetup({ traditional: true });

var params = {foo:"bar", baz:42, quux:"All your base are belong to us"};
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() {

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 ] };
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)" );

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?" };
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" );


});

0 comments on commit 3951894

Please sign in to comment.
You can’t perform that action at this time.