サーバーに次の投稿を作成しています。
$.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
}