开发者

jQuery.ajax() and sending boolean request arguments

开发者 https://www.devze.com 2023-02-09 11:50 出处:网络
$.ajax({ url : uri, type : \'post\', data : {someBooleanVar1: false, subVar: {someBooleanVar2: true}} });
$.ajax({
  url : uri,
  type : 'post',
  data : {someBooleanVar1: false, subVar: {someBooleanVar2: true}}
});

The problem is that on server someBooleanVar1 and someBooleanVa开发者_JAVA百科r2 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"?


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);


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);


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 :)


Not really automatically but adding 0 will convert the boolean to 0 or 1:

data : {someBooleanVar1: false + 0, someBooleanVar2: true + 0}


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 );


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消