将复杂对象传递到WCF Rest Service [英] Passing complex objects into a WCF Rest Service
问题描述
我有一个接受复杂对象的操作合同,我通过jQuery调用该操作.如何使用jQuery传递复杂类型的对象.下面是操作签名:
I have an Operation Contract that accepts a complex object and I'm calling the operation through jQuery. How do I pass in a complex type object like that using jQuery. Below is the operation signature:
public Resolution CreateNewResolution(Resolution NewResolution);
我需要在客户端上传递一个Resolution对象,但是我不知道如何使用jQuery.有帮助吗?
I need to pass in a Resolution object on the client, but I don't know how to do something like using jQuery. Any help?
谢谢
推荐答案
See Denny's post for a start, although I don't agree with his use of GET, and passing JSON in the querystring for complex params. That seems really wrong.
用于data
的参数是任何分辨率类型的json表示形式.例如,假设在服务器端这样定义类型和操作:
The param you use for data
is the json representation of whatever your Resolution type is. For example, suppose the type and operation is defined like this on the server side:
[DataContract( Namespace = "urn:brandon.michael.hunter/ws/2010/01",
Name = "Resolution" )]
public class Resolution
{
[DataMember( IsRequired = true, Name = "Name" )]
public string Name { get; set; }
[DataMember( IsRequired = true, Name = "Rank" )]
public int Rank { get; set; }
[DataMember( IsRequired = true, Name = "SerialNumber" )]
public int SerialNumber { get; set; }
[DataMember( IsRequired = false, Name = "Id" )]
public int Id { get; set; }
}
[OperationContract]
[WebInvoke(Method = "PUT",
RequestFormat=WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "new")]
public Resolution CreateNewResolution(Resolution r)
{
// your logic here
r.Id = System.Guid.NewGuid();
return r;
}
然后,在Javascript中,您使用的代码可能如下所示:
Then, in Javascript, the code you use might look like this:
var resolution = {r: { Name : "Fred", Rank : 2, SerialNumber : 17268 }};
// convert object to JSON string (See http://jollytoad.googlepages.com/json.js)
var objectAsJson = $.toJSON(resolution);
// result is a string: '{"Name":"Fred","Rank":"2","SerialNumber":"17268"}'
$.ajax({
type : "PUT", // must match Method in WebInvoke
contentType : "application/json",
url : "Service.svc/new", // must match UriTemplate in WebInvoke
data : objectAsJson,
dataFilter : function (data, type) {
// convert from "\/Date(nnnn)\/" to "new Date(nnnn)"
return data.replace(/"\\\/(Date\([0-9-]+\))\\\/"/gi, 'new $1');
},
processData : false, // do not convert outbound data to string (already done)
success : function(msg){ ... },
error : function(xhr, textStatus, errorThrown){ ... }
});
注意:
Notes:
- 至少在WCF 4中,您需要具有变量(r)的名称作为要传递的JSON中的第一个对象.开头的变量名称.
- 要在JSON中传递复杂对象,请使用PUT或POST作为请求的类型(HTTP方法)
- 您需要将复杂对象转换为JSON字符串.有一个一个不错的小巧的jquery插件来实现. 丹尼提供自己的实现.
- 我发现如果使用
processData=true
,则发送给服务的结果字符串将采用querystring格式,而不是JSON.不是我想要传递复杂对象的东西.因此我将其设置为false.对于正在执行WebGet的更简单的非JSON请求,使用true会很好,并且所有参数都在查询字符串中. - dataFilter允许对DateTime对象进行正确的反序列化
- 传递给成功回调的
msg
参数包含返回的json. - 您可能想使用URL重写器将.svc标记隐藏在请求URL中
- 在这种情况下,WCF服务使用webHttp行为,而不是enableWebScript.后者动态地生成Javascript代理来调用服务,但是您提出问题的方式使您似乎不希望这样做.
- You need to have the name of the variable (r) to be the first object in the JSON that is being passed, at least with WCF 4. When I used the previous example, it did not work until I put in the name of the variable at the beginning.
- For passing complex objects in JSON, use PUT or POST as the type (HTTP Method) of the request
- you need to convert the complex object to a JSON string. There's a nice, tiny jquery plugin to do this. Denny provides his own implementation.
- I found that if I use
processData=true
, then the resulting string sent to the service is in querystring format, not in JSON. Not what I want for passing complex objects. So I set it to false. Using true would be fine for simpler non-JSON requests where you're doing WebGet, and all the params are in the query string. - the dataFilter allows for correct deserialization of DateTime objects
- the
msg
param passed to the success callback contains the returned json. - You may want to use a URL rewriter to hide that .svc tag in the request URL
- in this case, the WCF service uses the webHttp behavior, not enableWebScript. The latter dynamically generates Javascript proxies to invoke the service, but the way you asked the question, makes it seem like you don't want that.
这篇关于将复杂对象传递到WCF Rest Service的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!