ASP.net Ajax has raised the Web service to such an important position that it has almost become the most recommended implementation of ASP.NET AJAX server-side logic, because Web services are inherently designed purely for business logic. As we all know, the WEB service does not provide a "fancy" user interface, but rather focuses on the logical implementation of the program, which coincides with the idea of "completely separating the presentation layer from the business layer" advocated by the ASP.net AJAX client programming model- The client's ASP.net ajax framework is used to handle all the interface and user interaction functions of the program, and the server side only provides pure data and does not involve any presentation style.
By using the Web service client Access Proxy that is automatically generated by the ASP.net Ajax asynchronous communication layer, invoking the Web service to fetch data in the presentation layer code (that is, JavaScript) becomes an unusually simple thing- Its elegance even makes us believe that Ajax programs can be written so easily.
Next, we'll look at a simple example program to learn how to use JavaScript to asynchronously invoke a Web service in a asp.net ajax application. For demonstration purposes, the function of the program is very simple: the user enters the name in the text box of the page, and then clicks the button next to it, as shown in Figure 3-1.
Figure 3-1 user enters their own name in the interface
The program will use the ASP.NET Ajax asynchronous communication layer to send the user's name to the server-side web Service in AJAX mode. The Web service then generates a greeting message on the server side based on the user's name and sends it back to the client, and after the client receives the server response, it displays the greeting, as shown in Figure 3-2.
Figure 3-2 Program displays greetings from the server
Let's start with the server-side Web service. Create a new Web service class named Simplewebservice, where you declare a common Web service method--sayhello (). The method will accept a parameter named name and generate a greeting message to return:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SimpleWebService : System.Web.Services.WebService
{
[WebMethod]
public string SayHello(string name)
{
return string.Format("Hello {0}!", name);
}
}