How to Call C# Method/Function Using jQuery Ajax

Introduction

I will use the jQuery.ajax() or $.ajax() method to call the C# method (WebMethod). The $.ajax() method does an asynchronous HTTP (Ajax) request. This method will take various parameters. So you will get the entire parameters list in a link: https://api.jquery.com/jQuery.ajax/

You need to add the jQuery library to your project in order to run this jQuery Ajax call. You can download the jQuery library from this link: http://jquery.com/download/

Here is the jQuery Script.

JQuery Script

Here is the C# function.

C# function

Output

Output

Note

  1. Here I declared the C# method as WebMethod and it should be static.
  2. If this method is not static then you will get an error and the method will never be called.
  3. Download the code to test this $.ajax() functionality.