Reputation: 5305
$.ajax({
url : uri,
type : 'post',
data : {someBooleanVar1: false, subVar: {someBooleanVar2: true}}
});
The problem is that on server someBooleanVar1 and someBooleanVar2 will be received as strings "false" and "true", but not as "0" and "1". Is there any way to automatically convert boolean arguments to "1" and "0"?
Upvotes: 7
Views: 10188
Reputation: 1017
Here's an even more optimized version that doesn't pollute the global namespace, converts values recursively, takes care of the potential undefined data property, and uses the proper method for converting boolean values to their corresponding integer values.
$.ajax = (function($ajax) {
return function(options) {
(function (obj) {
var _fn = arguments.callee;
$.each(obj, function(i) {
if (typeof obj[i] == 'object') {
_fn(this);
} else if (typeof obj[i] == 'boolean') {
obj[i] = ~~obj[i];
}
})
})(options.data || {});
return $ajax(options);
};
})($.ajax);
I still think it's quite shocking that this isn't performed internally by jQuery before it sends the request.
Upvotes: 0
Reputation: 6565
i know this post is a bit old but i still wanted to pass this information ^^ i pass vars to php and catch them with:
filter_var($var, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
like this i turn strings into booleans true=1
and false=
false as string is empty in php
maybe i read the question wrong. but this is what i understood :) with the code above you can easy build a function and add more filters to get everything work as you want :)
Upvotes: 1
Reputation: 494
This is a fix for Yi Jiang's answer. If you call ajax and don't have data set it gives an error. Added a test to see if the data property is set before converting the booleans. I used underscore, feel free to swap it out with the native js function hasownprop...
function convertBoolToNum( obj ) {
$.each( obj, function( i ) {
if ( typeof obj[i] == 'object' ) {
convertBoolToNum(this);
}
else if ( typeof obj[i] == 'boolean' ) {
obj[i] = Number( obj[i] );
}
} );
}
$.ajax = ( function( $ajax ) {
return function( options ) {
if ( _.has( options, 'data' ) ) {
convertBoolToNum( options.data );
}
return $ajax( options );
};
} )( $.ajax );
Upvotes: 0
Reputation: 5305
There is a fixed version of @jcubic Answer:
function convertBoolToNum(obj) {
$.each(obj, function(i) {
if (typeof obj[i] == 'object') {
convertBoolToNum(this);
}
else if (typeof obj[i] == 'boolean') {
obj[i] = Number(obj[i]);
}
});
}
$.ajax = (function($ajax) {
return function(options) {
convertBoolToNum(options.data);
return $ajax(options);
};
})($.ajax);
Upvotes: 2
Reputation: 66650
Try this, it should automatically convert booleans values to numbers in data options.
$.ajax = (function($ajax) {
return function(options) {
if (options.data != undefined) {
for (var i in options.data) {
if (options.data.hasOwnProperty(i) &&
(typeof options.data[i] == "boolean")) {
options.data[i] = Number(options.data[i]);
}
}
}
return $ajax(options);
};
})($.ajax);
Upvotes: 1
Reputation: 66397
Not really automatically but adding 0 will convert the boolean to 0 or 1:
data : {someBooleanVar1: false + 0, someBooleanVar2: true + 0}
Upvotes: 0