这是我第一次使用Knockout使用WCF服务。我想将整个视图模型作为带有ajax调用的JSON对象进行POST。
这是我收到的错误消息:
使用'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: {
}
})
}
}
})
}
答案 0 :(得分:1)
尝试将OperationContract
更改为这样......
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public bool AddNewEvent(Models.DAL_CRMEvents newEvent)
有人posted here
这解决了同样的问题。还有post here
可能有帮助。