用JQuery AJAX调用WCF服务
2009-10-25 00:00:00 来源:WEB开发网在网络上经常看到一些初学者提问怎么在AJAX中或用Javascript调用WCF服务,本文将简要给初学者介绍如何在AJAX中或用Javascript调用WCF服务以及注意事项。为了便于讲解,我们首先创建一个WCF服务,服务和服务数据定义如下:
//数据契约
[DataContract]
public class Person
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
}
//WCF服务,为了能使js调用,必须设置AspNetCompatibilityRequirements为Allowed或Required
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PersonService
{
//服务功能1
[OperationContract]
[WebGet]
public Person GetOnePerson()
{
return new Person { ID = 1, Name = "cokkiy" };
}
//服务功能2
[OperationContract]
[WebGet]
public List<Person> GetPorsons(int id, string name)
{
return new List<Person>() { new Person { ID=1,Name="cokkiy"},
new Person { ID=id,Name=name} };
}
}
更多精彩
赞助商链接