Click here to Skip to main content
15,893,904 members
Articles / Web Development / ASP.NET

ASP.NET 3.5, AJAX, and Web Services

Rate me:
Please Sign up or sign in to vote.
2.67/5 (5 votes)
17 Mar 2008CPOL6 min read 61.2K   1.2K   20  
This article explains how to call Web Service methods using AJAX client script.
using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Web.Script.Services;

namespace AjaxWebService
{

    [WebService(Namespace = "http://localhost:49232/AjaxWebService/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    public class Service : System.Web.Services.WebService
    {
        string myXmlData = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
                <Book>
                    <Title>Programming ASP.NET 3.5, AJAX and Web Services</Title>
                </Book>";
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        [WebMethod]
        public string getNextBackupDate(int months)
        {
            return DateTime.Now.AddMonths(months).ToShortDateString();
        }
        
        [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
        [WebMethod]
        public XmlDocument GetBookTitle()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(myXmlData);
            return xmlDoc;
        }
       
        [ScriptMethod(UseHttpGet = true)]
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello, world";
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder BB Systems CIT-GPNP
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions