Calling Web Service using AJAX

In the previous post, we saw how to use AJAX Control Toolkit in ASP.NET. In this post, we will see how to call a Web service using the AJAX. Let us say, we have a Service1.asmx which contains a method HelloWorld, which accepts a name of a person and greets  the person. For using the Service in AJAX, we need to register the Service inside the ScriptManager.

Let us say we just have HTML controls in the screen, a HTML textbox and HTML button.

Clicking on the Button will generate a Client side Javascript event. We can call the Service method inside the Client side event. In this case we will send the value of textbox and also set the returned value into the textbox.

Calling a Service method also makes us to register events in case of Success and Failure. SuccessMethod tells us what to do on completion of Service invocation with the argument as the result of Service Invocation,. Failure method advocates what to do in case of failure.