New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
jQuery.param() semantically incorrectly handles boolean properties #2619
Comments
That sounds like a pretty drastic backwards-incompatible change, which is very unlikely to land even in a new major release. It also seems easy enough to work around by using a custom |
Dependency: function filterObjForJqueryParam( data ) {
var obj = data instanceof Array ? [] : {};
for ( var i in data )
if ( data.hasOwnProperty( i ) )
switch ( typeof data[ i ] ) {
case 'boolean':
obj[ i ] = data[ i ] ? 1 : '';
break;
case 'object':
obj[ i ] = filterObjForJqueryParam( data[ i ] );
break;
default:
obj[ i ] = data[ i ];
}
return obj;
}
$.ajax({
url: 'http://example.com/',
type: 'POST',
data: filterObjForJqueryParam({ foo: true, bar: false })
}) A flag which controls output behavior can be passed to |
I think the docs say it well.
|
Boolean values can't be passed to query string explicitly. Because of it
jQuery.param()
turns boolean properties of input object to strings:true
is turned to'true'
andfalse
is turned to'false'
. For example:jQuery.param({ foo: true, bar: false })
returns'foo=true&bar=false'
(http://codepen.io/anon/pen/BoQXVg).In most cases server side scripts don't have ability to check whether query variable was originally boolean or string and auto conversion to boolean turns both
'true'
and'false'
totrue
. It would be great ifjQuery.param()
handles boolean properties like HTML checkbox element does: returnson
or1
if the state istrue
and''
(empty string) if the state isfalse
. For example:jQuery.param({ foo: true, bar: false })
would return'foo=1&bar='
.The text was updated successfully, but these errors were encountered: