这是我第一次使用带有 Knockout 的 WCF 服务。我想通过 ajax 调用将整个视图模型作为 JSON 对象发布。
这是我收到的错误消息:
使用“UriTemplate”的端点不能与“System.ServiceModel.Description.WebScriptEnablingBehavior”一起使用
我注意到一些开发人员将每个值作为参数发送,我认为这是不必要的,尤其是在您使用大对象时。
这是我的 WCF 方法:
[OperationContract]
[WebInvoke(UriTemplate = "AddNewEvent?newEvent", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public bool AddNewEvent(Models.DAL_CRMEvents newEvent)
{
Entities.CRMEntities dbCRM = new Entities.CRMEntities();
//Models.CRMEvents crmEvent = new Models.CRMEvents();
Entities.Event crmEvent = new Entities.Event();
crmEvent.EventDateCreated = Convert.ToDateTime(newEvent.DateCreated);
crmEvent.EventActive = true;
crmEvent.EventDescription = newEvent.Description;
crmEvent.EventDate = Convert.ToDateTime(newEvent.Date);
crmEvent.EventTimeStart = TimeSpan.Parse(newEvent.TimeStart);
crmEvent.EventTimeEnd = TimeSpan.Parse(newEvent.TimeEnd);
crmEvent.EventAllDay = newEvent.AllDay;
dbCRM.AddToEvent(crmEvent);
return true;
}
这是我的ajax函数
function SaveEvent (data) {
var s = {
newEvent: ko.mapping.toJS(data)
}
alert(data.AllDay());
$.ajax({
type: "POST",
url: "../Services/CRMDataService.svc/AddNewEvent",
data: JSON.stringify(s),
contentType: "application/json; charset=utf-8",
dataType: "JSON",
success: function (result) {
alert(result);
},
error: function (jqXHR, textStatus, errorThrown) {
if (textStatus == "error" && errorThrown != "") {
var n = noty({
text: errorThrown,
type: 'warning',
dismissQueue: false,
modal: true,
layout: 'center',
theme: 'defaults',
callback: {
}
})
}
}
})
}