将这样的字符串传递给WCF服务是否可以:
self.savePreferences = function(callback) {
$.ajax({
url: "services/Foo.svc/SavePreferences",
type: 'POST',
timeout: 3000,
data: ko.mapping.toJSON(self.user().preferences),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
if (result) {
alert("data saved " + result);
} else {
//todo, send server email alert
alert("Something went wrong. We will look into it.");
}
}
});
}
在这种情况下,我的服务中的jsonPreferences总是为null
[OperationContract]
[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json)]
string SavePreferences(string jsonPreferences);
或者我需要这样做:
data:JSON.stringify(ko.mapping.toJSON(self.user()。preferences))