Create a new web site, and add an Ajax-enable WCF Service in the site, for example, Service
After the file is created, there will be a CS file under appcode and an SVC file under the root directory. the backend file to which this file is directed is the CS file under appcode.
<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>
Open the CS file and modify the Code as follows:
Using system; using system. LINQ; using system. runtime. serialization; using system. servicemodel; using system. servicemodel. activation; using system. servicemodel. web; [servicecontract (namespace = "mynamespace")] // All namespaces of the class are defined here [aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. allowed)] public class service {[operationcontract] // exposes a Public String getmessage () {retu RN "Success returned by the WCF server !! ";}}
The preceding settings are partially complete (in fact, the configuration information of the system. servicemodel space is added to Web. config)
The following is called on the ASPX page:
Pull a scriptmanager and add the path of the WCF Service to it. The Code is as follows:
<Asp: scriptmanager id = "scriptmanager1" runat = "server"> <services> <asp: servicereference Path = "~ /Service. SVC "/> specify the SVC file path </services> </ASP: scriptmanager>
The JS Code on the same page is as follows:
<Script language = "JavaScript" type = "text/JavaScript"> function ajaxtest () {mynamespace. service. getmessage (onmethodcompleted); // here there are several methods that are overloaded to specify the callback function for success or failure.} function onmethodcompleted (results) {var inner = document. getelementbyid ("show"); inner. innertext = Results ;}</SCRIPT>
Pull a client button on the ASPX page and specify the onclick event as ajaxtest. When the button is clicked, the return value of the WCF method will be displayed...