Simple Application of Ajax and WCF

Source: Internet
Author: User

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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.