简体   繁体   English

jQuery.ajax()并发送布尔请求参数

[英]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". 问题是在服务器上someBooleanVar1和someBooleanVar2将作为字符串“false”和“true”接收,但不能作为“0”和“1”接收。 Is there any way to automatically convert boolean arguments to "1" and "0"? 有没有办法自动将布尔参数转换为“1”和“0”?

There is a fixed version of @jcubic Answer: 有一个固定版本的@jcubic答案:

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: 我知道这篇文章有点旧,但我仍然希望传递这些信息^^我将vars传递给php并抓住它们:

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 像这样我将字符串转换为布尔值true=1false= false,因为字符串在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. 如果您调用ajax并且没有数据集,则会出错。 Added a test to see if the data property is set before converting the booleans. 在转换布尔值之前添加了一个测试,以查看是否设置了data属性。 I used underscore, feel free to swap it out with the native js function hasownprop... 我使用下划线,随意用原生js函数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. 我仍然认为,在发送请求之前,这不是由jQuery在内部执行的,这是非常令人震惊的。

不是真的自动但添加0会将布尔值转换为0或1:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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