The following example shows how to make sa simple call to a web service method or a page method using JQuery.
.aspx code:
<%@ Page Language="C#" CodeBehind="JQuery1.aspx.cs" Inherits="Demo.JQuery.UI.WebApp.JQuery1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="_default" runat="server"> <asp:ScriptManager ID="_ScriptManager" runat="server" /> <script language="javascript" type="text/javascript" src="jquery.js"></script> JQuery<br /> <input type="button" id="btnGetDate" value="Get Date" onclick="btnGetDate_Click()" /> <input type="button" id="btnGetWelcome" value="Get Welcome" onclick="btnGetWelcome_Click()" /> <input type="button" id="btnGetMachineName" value="Get Machine Name" onclick="btnGetMachineName_Click()" /> </form> <script language="javascript" type="text/javascript"> //<!-- function btnGetDate_Click() { $.ajax({ type: 'POST' ,url: 'appservice.asmx/GetServerDateAndTime' ,contentType: 'application/json; charset=utf-8' ,dataType: "json" ,data: '{}' ,success: function(data, status) { alert(data); } ,error: function(xmlHttpRequest, status, err) { alert('Sorry! Error happens.'); } } ); } function btnGetWelcome_Click() { $.ajax({ type: 'POST' ,url: 'appservice.asmx/GetWelcomeMessage' ,contentType: 'application/json; charset=utf-8' ,dataType: "json" ,data: '{name:"John"}' ,success: function(data, status) { alert(data); } ,error: function(xmlHttpRequest, status, err) { alert('Sorry! Error happens.'); } } ); } function btnGetMachineName_Click() { $.ajax({ type: 'POST' ,url: 'jquery1.aspx/GetServerMachineName' ,contentType: 'application/json; charset=utf-8' ,dataType: "json" ,data: '{}' ,success: function(data, status) { alert(data); } ,error: function(xmlHttpRequest, status, err) { alert('Sorry! Error happens.'); } } ); } //--> </script> </body> </html>
.aspx.cs code
namespace Demo.JQuery.UI.WebApp { /// <summary> /// First sample /// </summary> public partial class JQuery1 : System.Web.UI.Page { /// <summary> /// Returns the server date and time /// </summary> /// <returns></returns> [WebMethod()] public static string GetServerMachineName() { return HttpContext.Current.Server.MachineName; } } }
Web Service code
namespace Demo.JQuery.UI.WebApp { /// <summary> /// Summary description for AppService /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService()] [ToolboxItem(false)] public class AppService : System.Web.Services.WebService { /// <summary> /// Returns the server date and time /// </summary> /// <returns></returns> [WebMethod()] public string GetServerDateAndTime() { return DateTime.Now.ToString("dd-MMM-yyyy hh:mm:ss"); } /// <summary> /// Returns the server date and time /// </summary> /// <returns></returns> [WebMethod()] public string GetWelcomeMessage(string name) { return string.Format("Welcome {0}!!!", name); } } }