This article mainly introduces jQuery's method for calling the WCF Service through ajax. It analyzes jQuery's ajax front-end call and the techniques related to the background interactive call of the WCF Service in the form of a complete example, A complete example is provided for readers to download. If you need it, you can refer to the following example to describe how jQuery implements ajax to call the WCF Service. We will share this with you for your reference. The details are as follows:
AJAX calls to the WCF Service are divided into two methods: Cross-origin and non-Cross-origin. Today we will first introduce the call methods under non-Cross-origin. The DEMO is written in VS2008.
After tests and research, it is found that AJAX calls to the WCF Service must meet the following conditions:
1. The communication method of wcf must use webHttpBinding.
2. Required Node Value
3. The service implementation must be marked
The Code is as follows:
[AspNetCompatibilityRequirements (RequirementsMode = AspNetCompatibilityRequirementsMode. Allowed)]
4. Add the following mark before the Method
The Code is as follows:
[WebInvoke (Method = "POST", BodyStyle = WebMessageBodyStyle. Bare, ResponseFormat = WebMessageFormat. Json)]
5. The parameter name passed in the ajax method must be the same as the parameter method name provided in the wcf Service.
The following code is written by myself. You need to pay attention to the color.
Server Configuration File Code
Server code
[ServiceContract] public interface IService1 {[OperationContract] string GetData (int value); [OperationContract] City GetDataUsingDataContract (City composite); [OperationContract] List
GetList (); [OperationContract] List
GetListData (List
List) ;}// use the data conventions described in the following example to add a composite type to a service operation. [DataContract] public class City {int seq = 0; string cityID; string ctiyName; [DataMember] public string CityID {get {return cityID;} set {cityID = value ;}} [DataMember] public string CityName {get {return ctiyName;} set {ctiyName = value;} [DataMember] public int Seq {get {return seq ;} set {seq = value ;}}}
Implementation Code
[AspNetCompatibilityRequirements (RequirementsMode = AspNetCompatibilityRequirementsMode. allowed)] public class Service1: IService1 {[WebInvoke (Method = "POST", BodyStyle = WebMessageBodyStyle. wrappedRequest, RequestFormat = WebMessageFormat. json, ResponseFormat = WebMessageFormat. json)] public string GetData (int value) {return string. format ("You entered: {0}", value) ;}# region IService1 member [WebInvoke (Method = "POST", BodyStyle = WebMessageBodyStyle. bare, ResponseFormat = WebMessageFormat. json)] public City GetDataUsingDataContract (City composite) {City c = new City (); c. cityID = composite. cityID; c. cityName = composite. cityName; c. seq = composite. seq; return c;} [WebInvoke (Method = "POST", BodyStyle = WebMessageBodyStyle. bare, ResponseFormat = WebMessageFormat. json)] public List
GetList () {List
List = new List
(); City cc = new City (); cc. cityID = "1"; cc. cityName = "Beijing"; cc. seq = 3; list. add (cc); City PC3 = new City (); cc1.CityID = "2"; cc1.CityName = "Shanghai"; cc1.Seq = 4; list. add (PC3); return list;} [WebInvoke (Method = "POST", BodyStyle = WebMessageBodyStyle. bare, ResponseFormat = WebMessageFormat. json)] public List
GetListData (List
List) {return list;} # endregion}
Client call code
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "WebForm1.aspx. cs" Inherits = "WcfServiceDemoOne. WebForm1" %>