Jquery 使用复杂类型的WCF服务

Jquery 使用复杂类型的WCF服务,jquery,json,wcf,Jquery,Json,Wcf,我有这样的服务: [ServiceContract] public interface IService { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "DoWork", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] Person DoWork(Person person); }

我有这样的服务:

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "DoWork", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    Person DoWork(Person person);
}
服务实施如下:

public class Service : IService
{
    public Person DoWork(Person person)
    {
        //To do required function
        return person;
    }
}
我的
Person
类型定义是:

 [DataContract]
 public class Person
 {
    [DataMember]
    public string Name { get; set; }       
 }
我尝试使用jQuery使用此服务:

   var data = { 'person': [{ 'Name': 'xxxxx'}] };

   $.ajax({
            type: "POST", 
            url: URL, // Location of the service
            data: JSON.stringify(data), //Data sent to server
            contentType: "application/json", // content type sent to server
            dataType: "json", //Expected data format from server
            processData: false,
            async: false,
            success: function (response) {                 
            },
            failure: function (xhr, status, error) {                   
                alert(xhr + " " + status + " " + error);
            }
        });

我可以使用此函数调用服务,但是服务方法
DoWork
的参数(
Person
object)始终为空。如何修复此问题?

您的JavaScript
数据
对象构造不正确-它应该是:
{'person':{'Name':'xxxxx'}

此外,您还可以选择构建JavaScript对象的替代方法。解决方案(我认为不太容易出错)是以更标准的方式构建对象(代码多一点,但不太容易混淆和出错-尤其是在对象具有高复杂性的情况下):

最后一件事是,您没有设置发送到服务操作和从服务操作发送的消息的正文样式:

[WebInvoke(... BodyStyle = WebMessageBodyStyle.Wrapped)]

显示您的
Person
类型定义。[DataContract]公共类Person{[DataMember]公共字符串名称{get;set;}}我已经尝试过了。现在它也不起作用了。DoWork(Person-Person)方法参数为空。我已经编辑了答案-我在
数据
对象构造中输入了一个错误。现在服务参数也仅为空。是否可以使用json格式传递参数,因为我必须使用java和antroid访问此服务。我知道这是可能的,但我没有说不可能。更重要的是,它应该会起作用。有关更多信息,请查看此处(第二段):。
[WebInvoke(... BodyStyle = WebMessageBodyStyle.Wrapped)]