ASP.NET Web API - Part Five

In Part 1Part 2, Part 3, and Part 4 of this series, we discussed HTTP Verbs and their implementation and custom method names. Now let’s discuss how to call Web API using jQuery Ajax.

Now in this, we will be designing a simple HTML page which will have a button and will display a list of values by calling web API Get() using jQuery Ajax call creating web API project as shown.

File - New Project - Select Asp.Net Web Application and add the name of the project, as shown below,

ASP.NET

Now, select Web API, as shown below.

ASP.NET

Now go to ValueController,

  1. public class ValuesController : ApiController  
  2.   {  
  3.       // GET api/values  
  4.       public IEnumerable<string> Get()  
  5.       {  
  6.           return new string[] { "value1""value2" };  
  7.       }  
  8.   
  9.       // GET api/values/5  
  10.       public string Get(int id)  
  11.       {  
  12.           return "value";  
  13.       }  
  14.   
  15.       // POST api/values  
  16.       public void Post([FromBody]string value)  
  17.       {  
  18.       }  
  19.   
  20.       // PUT api/values/5  
  21.       public void Put(int id, [FromBody]string value)  
  22.       {  
  23.       }  
  24.   
  25.       // DELETE api/values/5  
  26.       public void Delete(int id)  
  27.       {  
  28.       }  
  29.   }  

Now, Add HTML page by right clicking - add - HtmlPage

ASP.NET

ASP.NET

Now, add the below code in HtmlPage1.html,

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <meta charset="utf-8" />  
  5.     <title></title>  
  6.     <script src="Scripts/jquery-1.10.2.js"></script>  
  7.     <script type="text/javascript">  
  8.   
  9.        $(document).ready(function () {  
  10.            var ulList = $('#ulList');  
  11.   
  12.            $('#btn').click(function () {  
  13.                $.ajax({  
  14.                    type: 'GET',  
  15.                    url: 'http://localhost:15414/api/Values',  
  16.                    dataType: 'json',  
  17.                    success: function (data) {  
  18.   
  19.                        ulList.empty();  
  20.                        $.each(data, function (index, val) {  
  21.                            var fullName = val + ' ' + val;  
  22.                            ulList.append('<li>' + fullName + '</li>')  
  23.   
  24.                        });  
  25.                    }  
  26.   
  27.                });  
  28.   
  29.            });  
  30.   
  31.   
  32.        });  
  33.   
  34.     </script>  
  35.   
  36. </head>  
  37. <body>  
  38.     <input type="button" id="btn" value="Get List" />  
  39.         <ul id="ulList"></ul>  
  40. </body>  
  41. </html>  

Now, in the above code we need to add a reference of jQuery libraries. 

  1. <script src="Scripts/jquery-1.10.2.js"></script>  

Now, we are calling click event of btn on document.ready

  1. $('#btn').click(function () {}  

 

Now, on button click we are calling Web API Get() by providing URL as 'http://localhost:portno/api/Values' and specifying its type as Get. And onSuccess we are binding data to ulList.

Now, build and run the application and access HtmlPage1.html

Now, click on Get List button and you will get a list of values as shown in the below example.

ASP.NET
 
In the next article, we will be discussing CORS; i.e. Cross-origin resource sharing in Web API.


Similar Articles