Jquery JSON包装对象时的奇怪行为

Jquery JSON包装对象时的奇怪行为,jquery,json,Jquery,Json,我在我的服务器上发布以下帖子 $.ajax({ url: url, data: JSON.stringify({ SecretKey: e.Code, CommentId: e.Id, Direction: direction, VoteType:1}), type: "POST", contentType: "application/json;charset=utf-8", }); 当请求发出时,如下所示: {"Direction":{"Id":1,"Code

我在我的服务器上发布以下帖子

$.ajax({
    url: url,
    data: JSON.stringify({ SecretKey: e.Code, CommentId: e.Id, Direction: direction, VoteType:1}),
    type: "POST",
    contentType: "application/json;charset=utf-8",
});
当请求发出时,如下所示:

{"Direction":{"Id":1,"Code":"1234-5678-9012","Description":"This is 1 comment."},"VoteType":"1"}
为什么
Direction
会包装元素?注意
VoteType
不受影响?
VoteType
与其他变量之间的唯一区别在于
VoteType
是一个文本值,而不是引用对象

完整型号,如果有帮助:

var model = {
    Id: ko.observable(0),
    Code: ko.observable(""),
    Description: ko.observable(""),
    Comments: ko.observableArray(),
    vote: function (e, direction) {
    $.ajax({
        url: url,
        data: { SecretKey: e.Code, CommentId: e.Id, Direction: direction, VoteType:1},
        type: "POST",
        contentType: "application/json;charset=utf-8",
    });
    },
    secretVote: function (e, direction) {
        $.ajax({
            url: url,
            data: { SecretKey: e.Code, Direction: direction, VoteType:0},
            type: "POST",
            contentType: "application/json;charset=utf-8",
        });
    },
    comment: sendComment
}
这:

stringify({SecretKey:e.Code,CommentId:e.Id,Direction:Direction,VoteType:1})

可以产生:

{“方向”:{“Id”:1,“代码”:“1234-5678-9012”,“描述”:“这是1条注释”。},“VoteType”:“1”}

如果您的
e.code
e.Id
未定义(
SecretKey
CommentId
字段随后被删除),并且如果
direction
对象为:

{“Id”:1,“代码”:“1234-5678-9012”,“描述”:“这是一条注释”。}`


当您调用
JSON.stringify
时,它将尝试序列化所有内容
description
(由键
description
标识)指向具有内部属性的复杂对象,因此
JSON。stringify
会将其序列化为JSON
VoteType
是一个键,因此将被序列化为
VoteType

另外,您没有看到
SecretKey
CommentId
的原因是它们是
未定义的
,因此不会被
JSON.stringify
序列化

总之,这更多地与键的值有关,而不是键本身。在第一种情况下,
Direction
表示复杂对象,而在第二种情况下,
VoteType
表示整数


另一方面,您无需使用
JSON.stringify
序列化数据;jQuery会帮你做的。

我认为你不需要做
JSON.stringify
。jQuery为您处理这些。@Pythonian我也是这么想的,我最初也是这样想的,但是,如果没有
JSON.stringify
,我会在Web API方法中为对象接收一个空的对象服务器大小。我认为
direction
传入时是一个对象。密钥(Id、代码、描述)与缺少的密钥(SecretKey、CommentId)不匹配。请确保e.Code和e.Id正确defined@JuanMendes我发布的JSON是在FireBug中捕获的——在攻击服务器之前。
JSON.stringify(direction)
提供了什么<代码>{“Id”:1,“代码”:“1234-5678-9012”,“描述”:“这是一条评论。”},这就是为什么?