1.6. Creating a Simple Web Page with AJAX

Although this book focuses on the power of using AJAX with ASP.NET 3.5 and the AJAX capabilities this framework brings to AJAX development, it is also interesting to see how you could build a simple AJAX-enabled HTML page without the use of any of the aforementioned frameworks.

To show this in action, create an ASP.NET solution within Visual Studio 2008. The first step you can take is to build the Web service that will return a value to be loaded up through the AJAX process. The code (in C#) of the Web service is presented here in Listing 1-1.

Example 1-1. The Web service to communicate with the AJAX-enabled Web page
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {

    [WebMethod]
    public string HelloWorld() {
        return "Hello Ajax World!";
    }
}

This is a simple C# Web service and as you can see from the single WebMethod, it only returns a string with no required parameters on the input. The AJAX page that you create for this example works with this Web service, and calls the Web service using the HTTP-GET protocol. By default, ASP.NET disallows HTTP-GET and you have to enable this capability in the web.config file in order for this example to work. Listing 1-2 shows the additions you need to make to this file.

Example 1-2. Changing ...

Get Professional ASP.NET 3.5 AJAX now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.