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 2, 2019, 10:03 PM EDT
How to call c# code behind non static method using jquery ajax
Vivek KumarPosted Apr 28, 2016, 11:06 AM EDT
Good one
Kuppurasu NagarajTOP 100Posted Apr 17, 2016, 12:13 AM EDT
Nice Sharing..
Humayun Kabir MamunPosted Apr 16, 2016, 9:46 PM EDT
Nice...
Kumaresh RajalingamPosted Apr 16, 2016, 4:34 AM EDT
Good one
Debasis SahaTOP 100Posted Apr 15, 2016, 10:36 PM EDT
Nice One..
Rohit AiriPosted Apr 15, 2016, 7:32 PM EDT
nice share sir, i did it with same with my live server, but its giving me jquery error like- block somthing.
Rahul Kumar SaxenaTOP 100Posted Apr 15, 2016, 6:46 PM EDT
Good show
Vignesh ManiTOP 100Posted Apr 15, 2016, 12:29 PM EDT
Nice