我正在向我的服务器发布以下帖子,
$.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
}