Skip to content
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

Closed
Finesse opened this issue Sep 29, 2015 · 3 comments
Closed

jQuery.param() semantically incorrectly handles boolean properties #2619

Finesse opened this issue Sep 29, 2015 · 3 comments

Comments

@Finesse
Copy link

Finesse commented Sep 29, 2015

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' and false 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' to true. It would be great if jQuery.param() handles boolean properties like HTML checkbox element does: returns on or 1 if the state is true and '' (empty string) if the state is false. For example: jQuery.param({ foo: true, bar: false }) would return 'foo=1&bar='.

@Finesse Finesse changed the title jQuery.param semantically incorrectly handles boolean properties jQuery.param() semantically incorrectly handles boolean properties Sep 29, 2015
@jzaefferer
Copy link
Member

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 param implementation. Or is there a dependency on jQuery.param that you have to work around?

@Finesse
Copy link
Author

Finesse commented Sep 29, 2015

Dependency: jQuery.ajax(). I use this solution to process data object before passing it to jQuery.ajax():

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 jQuery.param(). Described problem can be controlled the similar way. If this change is still impossible I will continue use my solution.

@dmethvin
Copy link
Member

I think the docs say it well.

Note: Because there is no universally agreed-upon specification for param strings, it is not possible to encode complex data structures using this method in a manner that works ideally across all languages supporting such input. Use JSON format as an alternative for encoding complex data instead.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants