5

我使用Wcf ServiceJS

我通过 向我的服务发送信息AJAX

我在接受以下对象的服务中有一个函数:

  [DataContract]
    public class Business
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Desc { get; set; }        
        [DataMember]
        public List<BusinessService> Services { get; set; }
    }

BusinessService.cs:

   [DataContract]
    public class BusinessService
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Desc { get; set; }
        [DataMember]
        public int LongTime { get; set; }
    }

我解析我的 JS 中的对象JSON并将它们发送到服务。

这里是发送到 wcf 函数的 json:

"{"ManageBusiness":{"Name":"aaaaaa","Desc":"aaaaaaaaaaaa","Services":[{"Name":"aaaaaa","Desc":"aaaaaa","LongTime":30},{"Name":"aaaaaa1","Desc":"aaaaaa","LongTime":30}]}}"

Services除列表外,一切正常。

该列表作为空值出现在 wcf 函数中。

我不知道为什么会这样,我会感谢帮助?

4

4 回答 4

4

您不能List<T>从 JSON 数据实例化 a。您可以做的是替换:

[DataContract]
    public class Business
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Desc { get; set; }        
        [DataMember]
        public List<BusinessService> Services { get; set; }
    }

[DataContract]
    public class Business
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Desc { get; set; }        
        [DataMember]
        public BusinessService[] Services { get; set; }
    }

也就是说,替换List<T>Array of T

于 2013-10-06T07:42:40.637 回答
0

您可以使用WCF 服务跟踪查看器来帮助您查看和跟踪您从服务中获得的错误。

于 2013-10-11T22:43:56.670 回答
0

尝试这个,

var data = "{"ManageBusiness":{"Name":"aaaaaa","Desc":"aaaaaaaaaaaa","Services":[{"Name":"aaaaaa","Desc":"aaaaaa","LongTime":30},{"Name":"aaaaaa1","Desc":"aaaaaa","LongTime":30}]}}";

在 ajax 调用中添加以下内容

contentType: "application/json",
data: JSON.stringify({ business: data }),

下面的代码来接收数据

public ActionResult BusinessCall(Business business){}
于 2015-08-12T07:32:25.010 回答
0

我可以说以下内容绝对有效。我刚刚测试了它。对不起,它在 vb.net 中,但你应该明白。

在服务器端:在 IService1.vb

Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web

Namespace Webservices
   <ServiceContract()>
    Public Interface IService1

    <OperationContract()> _
    <WebInvoke(method:="POST", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.WrappedRequest)> _
    Function test(manageBusiness As ManagedBusiness) As string

   End Interface
End Namespace

在 Service1.svc.vb

Imports System.Runtime.Serialization
Imports System.ServiceModel.Activation
Namespace Webservices
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Required)>
Public Class Service1
    Implements IService1 
    Public function test(manageBusiness As ManagedBusiness) As string Implements IService1.test 
       Return "done"   
     End function    
End Class   

<CollectionDataContract()>
Public Class Services
    Inherits List(Of Service)
End Class

<DataContract()>
Public Class Service
    <DataMember()>Public Property Name As String
    <DataMember()>Public Property Desc As String
    <DataMember()>Public Property LongTime As Integer 
End Class

<DataContract()>
Public Class ManagedBusiness 
    <DataMember()>Public Property Name As String
    <DataMember()>Public Property Desc As String
    <DataMember()>Public Property Services As Services

End Class 
End NameSpace

我连接了 webconfig 设置。并将这个 jquery ajax 调用放在客户端:

$("#btnTest").click(function () {
            var services = [];
            services.push({
                Name: "aaaaaa",
                Desc: "aaaaaa",
                LongTime: 30
            });
            services.push({
                Name: "aaaaaa1",
                Desc: "aaaaaa",
                LongTime: 30
            });
            var data = {};
            data.manageBusiness = {
                Name: "aaaaaa",
                Desc: "aaaaaaaaaaaa",
                Services: services
            }


            $.ajax({
                type: "POST",
                url: "http://localhost:64110/Webservices/Service1.svc/test",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                async: true,
                data: JSON.stringify(data),
                success(msg) {

                }
            });


        });

我可以确认请求的有效负载如下:

{"manageBusiness":{"Name":"aaaaaa","Desc":"aaaaaaaaaaaa","Services":[{"Name":"aaaaaa","Desc":"aaaaaa","LongTime":30},{"Name":"aaaaaa1","Desc":"aaaaaa","LongTime":30}]}}

WCF 服务反序列化一切都很好。我认为您缺少的是 [CollectionDataContract]属性。

更新:我尝试删除 [CollectionDataContract] 属性,它仍然有效。所以我猜测数据契约属性仅用于序列化,不需要反序列化(?)

于 2015-08-12T16:20:55.627 回答