我的WCF有一个插入方法,如下所示:
public void InsertWidget(WidgetDef widgetDef)
class WidgetDef
{
[Key]
int widgetID;
string title;
int x;
int x;
// there are more properties, but I think you get the idea...
}要通过JSON端点访问它,我想我需要向url发布一个变更集:
serverURLWidgetService.svc/json/SubmitChanges.
我很确定我的URL是正确的,因为我的请求到达了WidgetService.Initialize方法,但是我在服务器中得到了一个异常--这并不奇怪,因为我不知道请求的内容应该是什么样子。
我的问题:用于插入操作的HTTP请求内容的(JSON)格式是什么?
发布于 2011-11-21 08:09:05
给出的示例插入changeset如下所示:
{"changeSet":[
{"Id":0,
"Entity":{"__type":"WidgetDef:#widgetDefNamespace",
"widgetId":0,
"title":"the new title",
"x":10,
"y":10,
},
"Operation":2 // '2' for insert, '3' for update, '4' for delete
}
]
} 感谢以下博客文章:http://www.joseph-connolly.com/blog/post/WCF-RIA-Services-jQuery-and-JSON-endpoint-Part-2.aspx
发布于 2016-07-13 01:42:59
这是一个非常晚的答案,但以防其他人再次遇到这些问题;重要的是,__type是实体中的第一个键。
我遇到了一些异常,比如:This DomainService does not support operation 'Update' for entity 'Object',它表明Domain无法解析实体的类型,因此无法找到适当的处理程序。
研究人员发现了这篇关于http://www.blog.yumasoft.com/node/108主题的博客,其中包含了解决方案。
我想指出,这种行为违反了JSON规范(参见:https://stackoverflow.com/a/5525820/1395343)。
一个可能的解决办法是使用replace确保__type在正确的位置结束。我不相信这是个好主意,但确实有效。
var entityChange = {};
entityChange.Id = 0;
entityChange.Operation = 3;
entityChange.Entity = {'key': 'Something that changed'};
var payload = JSON.stringify({ changeSet: [entityChange]});
// This is not an ideal way of doing this.
payload = payload.replace('"Entity":{', '"Entity":{"__type":"TypeName:#Namespace.Stuff",');
return $.ajax({
url: "...Web.svc/JSON/SubmitChanges",
method: "POST",
data: payload,
contentType: "application/json",
dataType: "json",
processData: false,
});https://stackoverflow.com/questions/8179504
复制相似问题