简体   繁体   中英

jQuery.ajax() and sending boolean request arguments

$.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"?

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

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

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

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会将布尔值转换为0或1:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM