Introduction: In this article we will learn to use (consume) a web service in our web application using Ajax (Asynchronous JavaScript and XML). We use Ajax in those applications where we do not want the page to be reloaded for a specific event. Here at first we will create a simple web service with two methods - One for showing message and another for adding two numbers and then we will use this web service into web application using Ajax. So, Let's create a web service. Follow given steps.
Now modify the code of .asmx.cs with the following code. using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; namespace wsuingAjax { /// <summary> /// Summary description for Service1 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public class Service1 : System.Web.Services.WebService { [WebMethod(Description="Show Message")] public string show() { return "Hello Web Service!!!"; } [WebMethod(Description = "Addition of Two Numbers")] public int add(int a, int b) { return a + b; } } } Now run the application. Output: Now we use this service using Ajax in our web application. Follow the given steps.
Output:
Consuming a Web Service in a Windows Application - 2nd Method
MessageName property of WebMethod in Web Services