为什么jQuery ajax不能序列化我的对象? [英] Why jQuery ajax does not serialize my object?
问题描述
我正在使用jQuery 1.7.1.并且我有以下问题/问题:
I am using jQuery 1.7.1. and I have the following question/problem:
代码如下:
var accountObject = new Object();
accountObject.account = new Object();
accountObject.account.sas = [];
$.ajax({
type: "PUT",
url: "/a/2",
data: accountObject,
dataType: "html",
success: function(msg) {
alert_user("Successfully saved the selected session attributes as defaults");
},
error: function(msg) {
alert_user(msg);
}
});
问题是,由于我将accountObject.account.sas
设置为一个空数组,因此data
在服务器上完全变为空,如下所示:
The problem is that, due to the fact that I set accountObject.account.sas
to an empty array, the data
arrives completely empty at the server, as:
{}
它应该在的位置:
{ "account": { "sas": [] } }
当我在account
的sas
属性中放置一些条目时,一切正常.例如,如果将字符串"1","2"和"3"作为数组["1", "2", "3"]
放置,那么我将进入服务器:
When I put some entries in the sas
property of account
then everything is ok. For example, if I put the string "1", "2" and "3" as an array ["1", "2", "3"]
then I get on the server:
{ "account": { "sas": [ "1", "2", "3" ] } }
有人知道为什么会这样吗?
Does anybody know why this is so?
先谢谢了 帕纳约提斯(Panayotis)
Thanks in advance Panayotis
推荐答案
问题是无法在查询字符串中正确表示空数组.当您将对象层次结构传递给data
时,jQuery将尝试将其转换为查询字符串,但无法弄清楚如何使用空数组.
The problem is that an empty array cannot be properly represented in a query string. When you pass the object hierarchy to data
, jQuery will attempt to convert it to a query string, but can't figure out what to do with an empty array.
1.4.4每当传递相同的层次结构时似乎都返回一个空字符串的原因是,它试图将键传递给不带值的查询字符串,其结果将类似于:...&property=&
.在大多数服务器环境中,默认情况下将其解释为空字符串.
The reason why 1.4.4 seemed to return an empty string whenever it got passed the same hierarchy was that it attempted to pass the key to the query string without a value, the result of which would be like: ...&property=&
. This is by default interpreted as an empty string in most server environments.
我个人认为1.7.1的行为是正确的,因为它迫使开发人员检查参数是否存在,而不是改变参数的类型.
I personally consider the behavior of 1.7.1 correct, because it forces the developer to check for parameter existence instead of mucking the type of the parameter.
一些有关此处提到的各种事情的注释
-
traditional: true
是完全错误的,因为它永远无法处理对象层次结构.相反,您得到的是:...&key=[object Object]
,这是javascript的toString()
所有对象的默认结果.
traditional: true
is completely wrong, since it can never handle object hierarchies. What you get instead is:...&key=[object Object]
, which is javascript'stoString()
default result for all objects.
JSON.stringify
将导致整个内容作为JSON传递到查询字符串,除非您将其与processData: false
结合使用,在这种情况下,您需要手动"反序列化为有意义的对象在服务器端.通常,这是一个有用的选项,但是在这种情况下,如果简单地进行一次检查就可以使事情工作的麻烦降到最低,那就过分了.
JSON.stringify
assigned to data
will result in the entire thing passed as JSON to the query string, unless you combine it with processData: false
, in which case you need to deserialize into meaningful objects "by hand" on the server side. It's generally a useful option, but overkill in this case where a simple check would presumably have things working with minimal hassle.
这篇关于为什么jQuery ajax不能序列化我的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!