How to call Server Side methods(code behind method) at Client Side in ASP.Net using jQuery AJAX


JQuery : allows you to call Server Side Code-Behind methods from client side without any PostBack.
  • jQuery.Ajax() or $.Ajax() performs an asynchronous http request. This method can handle response type of xml, json, script or html. You can perform asynchronously GET, POST, PUT or DELETE methods on server using Ajax.
Let us consider an example : I have a login page and on click of login button i want to validate username and password using AJAX call.

 To create a project please refer How to create a project in ASP.NET

Step 1: Create a web page which should have 2 fields (i.e username and password ) and a button for login.

















Step 2: Create a web method/page method in code-behind page where you will validate username and  password. the method should be a static method or else it won't be called.






Step 3: Write AJAX method on button click , inside the <script> tag. Note: Don't forget to include  jquery.min.js



















Step 4: Press F5/Run the project and Check the output.

Note: Disable the button until you get a response from the server
(i.e. Disable the button before the starting of AJAX method and enable it back in Success function of the ajax method )to prevent multiple clicks.


Comments