The example in this article describes the way jquery implements the Ajax invocation of WCF services. Share to everyone for your reference, specific as follows:
About Ajax call WCF services are divided into Cross-domain and not cross-domain two ways, today we first introduce the next not Cross-domain call method. The demo was written in VS2008.
After testing and research, it is found that the Ajax invoke WCF service must meet the following conditions
1.WCF means of communication must use WebHttpBinding
2. You must set the value of the <endpointBehaviors> node
3. The implementation of the service must add tags
Copy Code code as follows:
[Aspnetcompatibilityrequirements (Requirementsmode = aspnetcompatibilityrequirementsmode.allowed)]
4. The method must precede the following markup
Copy Code code as follows:
[WebInvoke (method = "POST", bodystyle = webmessagebodystyle.bare, Responseformat = Webmessageformat.json)]
The parameter names passed in the 5.ajax method must match the parameter method names provided in the WCF service
The following is the code I wrote, mark the color is the place to pay attention to
Server-side configuration file code
<system.serviceModel> <services> <service name= "Wcfservicedemoone.service1" behaviorconfiguration= " Wcfservicedemoone.service1behavior "> <!--Service endpoints--> <endpoint address=" "binding=" WebHttpB Inding "contract=" Wcfservicedemoone.iservice1 "behaviorconfiguration=" Httpbehavior "></endpoint> < Endpoint address= "Mex" binding= "mexHttpBinding" contract= "IMetadataExchange"/>
Server-side code
[ServiceContract]
Public interface IService1
{
[OperationContract]
string GetData (int value);
[OperationContract]
City Getdatausingdatacontract (city composite);
[OperationContract]
List<city> getlist ();
[OperationContract]
List<city> Getlistdata (list<city> List);
}
Use the data contract 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 = We Bmessageformat.json, Responseformat = Webmessageformat.json)] public string GetData (int value) {return STRING.F
Ormat ("you entered: {0}", value); #region IService1 Member [WebInvoke (method = "POST", bodystyle = webmessagebodystyle.bare, Responseformat = Webmessage
Format.json)] The 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<city> getlist () {list<city> List = new list<city> ();
City cc = new City (); Cc.
Cityid = "1"; Cc. Cityname="Beijing"; Cc.
Seq = 3; List.
ADD (CC);
City cc1 = new City (); Cc1.
Cityid = "2"; Cc1.
CityName = "Shanghai"; Cc1.
Seq = 4; List.
ADD (CC1);
return list; [WebInvoke (method = ' POST ', bodystyle = webmessagebodystyle.bare, Responseformat = Webmessageformat.json)] Public
List<city> getlistdata (list<city> list) {return list;
} #endregion}
Client calling code
<%@ Page language= "C #" autoeventwireup= "true" codebehind= "WebForm1.aspx.cs" inherits= "Wcfservicedemoone.webform1" "%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Full instance code code click here to download the site.
I hope this article will help you with the jquery program design.