Commit 39518945 authored by Ben Alman's avatar Ben Alman Committed by John Resig

Moved jQuery.param "traditional" flag into jQuery.ajaxSettings, can now be...

Moved jQuery.param "traditional" flag into jQuery.ajaxSettings, can now be overridden via 2nd argument to jQuery.param
parent 7d0c1803
...@@ -40,7 +40,7 @@ jQuery.fn.extend({ ...@@ -40,7 +40,7 @@ jQuery.fn.extend({
// Otherwise, build a param string // Otherwise, build a param string
} else if ( typeof params === "object" ) { } else if ( typeof params === "object" ) {
params = jQuery.param( params ); params = jQuery.param( params, jQuery.ajaxSettings.traditional );
type = "POST"; type = "POST";
} }
} }
...@@ -172,6 +172,7 @@ jQuery.extend({ ...@@ -172,6 +172,7 @@ jQuery.extend({
data: null, data: null,
username: null, username: null,
password: null, password: null,
traditional: false,
*/ */
// Create the request object; Microsoft failed to properly // Create the request object; Microsoft failed to properly
// implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available // implement the XMLHttpRequest in IE7, so we use the ActiveXObject when it is available
...@@ -204,7 +205,7 @@ jQuery.extend({ ...@@ -204,7 +205,7 @@ jQuery.extend({
// convert data if not already a string // convert data if not already a string
if ( s.data && s.processData && typeof s.data !== "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 // Handle JSONP Parameter Callbacks
...@@ -594,12 +595,14 @@ jQuery.extend({ ...@@ -594,12 +595,14 @@ jQuery.extend({
// Serialize an array of form elements or a set of // Serialize an array of form elements or a set of
// key/values into a query string // key/values into a query string
param: function( a ) { param: function( a, traditional ) {
var s = [], var s = [];
// Set jQuery.param.traditional to true for jQuery <= 1.3.2 behavior. // Set traditional to true for jQuery <= 1.3.2 behavior.
traditional = jQuery.param.traditional; if ( traditional === undefined ) {
traditional = jQuery.ajaxSettings.traditional;
}
function add( key, value ) { function add( key, value ) {
// If value is a function, invoke it and return its value // If value is a function, invoke it and return its value
...@@ -615,8 +618,8 @@ jQuery.extend({ ...@@ -615,8 +618,8 @@ jQuery.extend({
}); });
} else { } else {
// If jQuery.param.traditional is true, encode the "old" way (the // If traditional, encode the "old" way (the way 1.3.2 or older
// way 1.3.2 or older did it), otherwise encode params recursively. // did it), otherwise encode params recursively.
jQuery.each( a, function buildParams( prefix, obj ) { jQuery.each( a, function buildParams( prefix, obj ) {
if ( jQuery.isArray(obj) ) { if ( jQuery.isArray(obj) ) {
......
...@@ -258,9 +258,9 @@ test("serialize()", function() { ...@@ -258,9 +258,9 @@ test("serialize()", function() {
}); });
test("jQuery.param()", 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"}; 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" ); 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() { ...@@ -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 ] }; 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" ); 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"}; 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" ); 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() { ...@@ -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 ] }; 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)" ); 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" );
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment