It is quite a common situation to call a server side method from client side using jQuery. In this article we will learn how we can call a simple method in the code behind file from jQuery.
To start with we create a new ASP.NET project and add a web page named WebMethodCall.aspx. Next, we add a textbox in the html of this page. Add reference to the jQuery from online. We will get data from server using the click of this button and display it in alert box. So our markup will look like the following:
- <head runat="server">
- <title></title>
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <input type="button" title="Click to call server method" value="Get data from server" />
- </div>
- </form>
- </body>
Next, add the following script to call the server method from client side.
- <script type="text/javascript">
- function GetData() {
- $.ajax({
- type: "POST",
- url: "WebMethodCall.aspx/GetData",
- data: '',
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- success: function (response) {
- alert(response.d);
- },
- });
- }
- </script>
- [System.Web.Services.WebMethod]
- public static string GetData()
- {
- return DateTime.Now.ToString();
- }
Happy coding...!!!
Read more articles on jQuery:

Umashankar RajpootPosted Aug 3, 2019, 2:03 AM
How to call c# code behind non static method using jquery ajax
Vivek KumarPosted Apr 28, 2016, 3:06 PM
Good one
Kuppurasu NagarajPosted Apr 17, 2016, 4:13 AM
Nice Sharing..
Humayun Kabir MamunPosted Apr 17, 2016, 1:46 AM
Nice...
Kumaresh RajalingamPosted Apr 16, 2016, 8:34 AM
Good one
Debasis SahaPosted Apr 16, 2016, 2:36 AM
Nice One..
Rohit AiriPosted Apr 15, 2016, 11:32 PM
nice share sir, i did it with same with my live server, but its giving me jquery error like- block somthing.
Rahul Kumar SaxenaPosted Apr 15, 2016, 10:46 PM
Good show
Vignesh ManiPosted Apr 15, 2016, 4:29 PM
Nice