From: Timmy Willison <4timmywil@gmail.com> Date: Wed, 20 Jun 2018 16:07:44 +0000 (-0400) Subject: Serialize: jQuery.param: return empty string when given null/undefined X-Git-Tag: 3.4.0~51 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0645099e027cd0e31a828572169a8c25474e2b5c;p=jquery.git Serialize: jQuery.param: return empty string when given null/undefined Fixes gh-2633 Close gh-4108 --- diff --git a/src/serialize.js b/src/serialize.js index 30fcf98cc..44d3606b3 100644 --- a/src/serialize.js +++ b/src/serialize.js @@ -70,6 +70,10 @@ jQuery.param = function( a, traditional ) { encodeURIComponent( value == null ? "" : value ); }; + if ( a == null ) { + return ""; + } + // If an array was passed in, assume that it is an array of form elements. if ( Array.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) { diff --git a/test/unit/serialize.js b/test/unit/serialize.js index 9d0afd7c3..604d72a21 100644 --- a/test/unit/serialize.js +++ b/test/unit/serialize.js @@ -1,7 +1,7 @@ QUnit.module( "serialize", { teardown: moduleTeardown } ); QUnit.test( "jQuery.param()", function( assert ) { - assert.expect( 23 ); + assert.expect( 24 ); var params; @@ -72,6 +72,9 @@ QUnit.test( "jQuery.param()", function( assert ) { params = { "test": [ 1, 2, null ] }; assert.equal( jQuery.param( params ), "test%5B%5D=1&test%5B%5D=2&test%5B%5D=", "object with array property with null value" ); + + params = undefined; + assert.equal( jQuery.param( params ), "", "jQuery.param( undefined ) === empty string" ); } ); QUnit.test( "jQuery.param() not affected by ajaxSettings", function( assert ) {