The common Ajax program is HTML + ashx, which requires a lot of communication code. However, Microsoft's WCF integrates communication code and simplifies the code.
1. Create a New WCF Server Page, and create a method and data class for the client.
Using system; using system. LINQ; using system. runtime. serialization; using system. servicemodel; using system. servicemodel. activation; using system. servicemodel. web; namespace webapplication1 {[servicecontract (namespace = "")] [aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. allowed)] public class wcfservice1 {// <summary> // obtain user information /// </Summary> /// <Param name = "ID"> User ID </param>/ // <returns> User class for storing User Information </returns> [operationcontract] public user getuser (int id) {return new user () {username = "Xiaojiang", age = 22, address = "Wenzhou, Zhejiang "};} // Add the [webget] attribute to use http get [operationcontract] public void dowork () {// Add the operation here to implement return ;} // Add more operations here and use [operationcontract] to mark them} // <summary> // define the user class, store user data // </Summary> public class user {Public String username {Get; set;} public int age {Get; set;} Public String address {Get; set ;}}}
2. Add An ASPX page, add a scriptmanager control at the top of the page, and add the server page in the data set of the control,
Finally, it is very convenient to write JS Code just like writing C # programs.
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "wcfajax. aspx. cs" inherits = "webapplication1.wcfajax" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">