什么是 JQuery 中的“参数序列化的传统风格”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5497093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:15:45  来源:igfitidea点击:

What is "traditional style of param serialization' in JQuery

jquery

提问by Ricky

Do you know what is "traditional style of param serialization" for jQuery.ajax() as mentioned in http://api.jquery.com/jQuery.ajax/?

您知道http://api.jquery.com/jQuery.ajax/ 中提到的 jQuery.ajax() 的“参数序列化的传统风格”是什么吗?

Can you give some introduction?

可以介绍一下吗?

Thanks

谢谢

回答by Felix Kling

Have a look at the documentation of jQuery.param():

看看文档jQuery.param()

As of jQuery 1.4, the $.param()method serializes deep objects recursively to accommodate modern scripting languages and frameworks such as PHP and Ruby on Rails. You can disable this functionality globally by setting jQuery.ajaxSettings.traditional = true;.

从 jQuery 1.4 开始,该$.param()方法递归地序列化深层对象以适应现代脚本语言和框架,例如 PHP 和 Ruby on Rails。您可以通过设置全局禁用此功能jQuery.ajaxSettings.traditional = true;

Given

给定的

var p = {foo: [1,2,3], bar: 42};

setting traditionalto truegenerates

设置traditionaltrue生成

foo=1&foo=2&foo=3&bar=42

While e.g. Python can handle these parameters, i.e. it generates a list for foo, PHP will only consider the last fooparameter.

虽然例如 Python 可以处理这些参数,即它生成一个列表 for foo,但 PHP 将只考虑最后一个foo参数。

But now by default, the result of the serialization is (actually it is URI encoded)

但是现在默认情况下,序列化的结果是(实际上是URI编码的)

foo[]=1&foo[]=2&foo[]=3&bar=42

which can be better handled, as mentioned, by PHP and RoR.

如前所述,可以通过 PHP 和 RoR 更好地处理。



Or maybe even more interesting is this. Given:

或者更有趣的是这个。鉴于:

var p = {foo: {1: [3,4], 2:2,3:3}, bar: 42};

traditional produces:

传统产品:

foo=[object Object]&bar=42

which is clearly not useful in comparison with the "new" way:

与“新”方式相比,这显然没有用:

foo[1][]=3&foo[1][]=4&foo[2]=2&foo[3]=3&bar=42