首页 新闻 会员 周边

关于WCF接收AJAX请求,Json数据的解析。

0
悬赏园豆:50 [已关闭问题] 关闭于 2015-06-30 10:18
 $('#btn4').bind('click', function () {
var did = { ID: 99999, Name: 'aaa' }; $.ajax({ url: uri + 'GetPersonByPerson', type: 'get', dataType: 'jsonp', contentType: "application/json", data: did, cache: false, timeout: 10000, success: function (data) { alert(data.Name); } }); });

前端页面调用wcf接口,入参是引用类型。

    [DataContract]
    public class Person
    {
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Name { get; set; }
    }


  public interface IServices
    {
        [OperationContract]
        [WebGet(ResponseFormat=WebMessageFormat.Json)]
        Person GetPersonByPerson(Person person);
    }

但是wcf接收到请求后,解析不出Person的值。person一直为null。为什么person会是null呢,ajax请求的时候,已经有入参   var did = { ID: 99999, Name: 'aaa' };为什么呢?求园友帮忙。

 

 public class TestServices : IServices
    {   
        public Person GetPersonByPerson(Person person)//这里始终是person==null
        {
            if (person != null)
                person.Name = "i am No." + person.ID.ToString();
            else
                person = new Person() { ID=-1,Name="i am No.-1" };

            return person;
        }
    }

下面是我的WCF的配置。

 1 <?xml version="1.0"?>
 2 <configuration>
 3   <appSettings/>
 4   <system.web>
 5     <compilation debug="true" targetFramework="4.0"/>
 6     <httpRuntime/>
 7   </system.web>
 8   <system.serviceModel>
 9     <standardEndpoints>
10       <webHttpEndpoint>
11         <standardEndpoint crossDomainScriptAccessEnabled="true"/>
12       </webHttpEndpoint>
13     </standardEndpoints>
14     <bindings>
15       <webHttpBinding>
16         <binding crossDomainScriptAccessEnabled="true"/>
17       </webHttpBinding>
18     </bindings>
19     <behaviors>
20       <endpointBehaviors>
21         <behavior name="ajaxbehaviors">
22           <enableWebScript/>
23         </behavior>
24       </endpointBehaviors>
25       <serviceBehaviors>
26         <behavior>
27           <serviceMetadata httpGetEnabled="true"/>
28         </behavior>
29       </serviceBehaviors>
30     </behaviors>
31     <services>
32       <service name="API.TestServices">
33         <endpoint address="" binding="webHttpBinding" contract="API.IServices" behaviorConfiguration="ajaxbehaviors"/>
34       </service>
35     </services>
36   </system.serviceModel>
37   <system.webServer>
38     <modules runAllManagedModulesForAllRequests="true"/>   
39     <directoryBrowse enabled="true"/>
40   </system.webServer>
41 </configuration>
View Code
SooLazy的主页 SooLazy | 菜鸟二级 | 园豆:226
提问于:2015-06-29 21:16
< >
分享
所有回答(1)
0

已经解决是,是json入参的问题。这样写就没问题了。但是如果层次比较深的话,又是个麻烦吧。

var did = { person: '{ "ID": 99999, "Name": "aaa" }' };

 

SooLazy | 园豆:226 (菜鸟二级) | 2015-06-30 10:17
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册