none
javascript 调用WCF,命名空间问题。 RRS feed

  • 问题

  • namespace WebApplication3.tom { [ServiceContract(Namespace = "MyWCF")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Service1 { [WebGet(RequestFormat=WebMessageFormat.Json)] [OperationContract] public People DoWork(People p) { People pp = new People(); pp.NameArray = new string[] { "haha","hehe","hoho"}; return pp; } [WebGet(RequestFormat = WebMessageFormat.Json)] [OperationContract] public Animal Hello(Animal a) { string ha = a.Name; Animal b = new Animal(); b.Name = "daxiang"; return b; } } [DataContract] public class People { [DataMember] public string[] NameArray; } [DataContract] public class Animal { [DataMember] public string Name; } }
    <pre lang="x-js"><pre lang="x-xml"><asp:ScriptManager ID="SM1" runat="server"> <Services> <asp:ServiceReference Path="~/Service1.svc" /> </Services> </asp:ScriptManager>
    function Check() { var proxy = new WebApplication3.tom.Service1(); var people = new Object(); people.NameArray = new Array(); people.NameArray.push("A"); people.NameArray.push("B"); people.NameArray.push("C"); proxy.DoWork(people,Done,Fail); } function Done(result) { debugger; var dfa = result; } function Fail() { }

    以上是页面引用wcf和javascript函数以及后台wcf代码,

    问题是'WebApplication3' is undefined  总报这个错,如果命名空间是一个单词,没有.的那种 就没问题,这个怎么解决啊


    Dude....
    2011年5月31日 5:04

答案

全部回复

  • namespace WebApplication3.tom
    {
      [ServiceContract(Namespace = "MyWCF")]
      [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
      public class Service1
      {
        [WebGet(RequestFormat=WebMessageFormat.Json)]
        [OperationContract]
        public People DoWork(People p)
        {
          
          People pp = new People();
          pp.NameArray = new string[] { "haha","hehe","hoho"};
          return pp;
        }
    
        [WebGet(RequestFormat = WebMessageFormat.Json)]
        [OperationContract]
        public Animal Hello(Animal a)
        {
          string ha = a.Name;
          Animal b = new Animal();
          b.Name = "daxiang";
          return b;
        }
      }
      [DataContract]
      public class People 
      {
        [DataMember]
        public string[] NameArray;
      }
      [DataContract]
      public class Animal 
      {
        [DataMember]
        public string Name;
      }
    }
    

    Dude....
    2011年5月31日 5:05
  •  function Check() {
          var proxy = new WebApplication3.tom.Service1();
          var people = new Object();
          people.NameArray = new Array();
          people.NameArray.push("A");
          people.NameArray.push("B");
          people.NameArray.push("C");
          proxy.DoWork(people,Done,Fail);
        }
        function Done(result) {
          debugger;
          var dfa = result;
        }
        function Fail() {
    
        }
    

    Dude....
    2011年5月31日 5:06
  • 你尝试下面的办法

     

    1.把[ServiceContract(Namespace = "MyWCF")]
    删除。

    2.再不行的话,尝试去掉Namespace,先测试一下,看能否成功

     

    记住每次 修改代码最好编译一次,然后重新启动调试


    Frank Xu Lei--谦卑若愚,好学若饥
    专注于.NET平台下分布式应用系统开发和企业应用系统集成
    Focus on Distributed Applications Development and EAI based on .NET
     

    老徐的网站】:http://www.frankxulei.com/

    微软WCF中文技术论坛
    微软WCF英文技术论坛

    Windows Azure中文技术论坛

    2011年6月2日 3:30
    版主