Web API With AJAX: Use GetJSON() Function to Get JSON Data

This article explains the getJSON() function of jQuery and how to use the getJSON() function to consume JSON data from the Web API.

This is the "Web API with AJAX" article series. In this article series we are explaining Web API and ajax() method to consume it. We have learned the POST, GET, PUT and DELETE methods. You can read them here.
This article explains the getJSON() function of jQuery and how to use the getJSON() function to consume JSON data from the Web API. We know that the Web API function can any type of data, in other words the content negotiation happens very smoothly. So, let's try a few examples.
 
Client code to implement getJSON() function
 
In this example we will configure the getJSON() function that will consume data from the Web API. Have a look at the following code.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>   
  2. <!DOCTYPE html>   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6.     <script src="jquery-1.7.1.js" type="text/javascript"></script>  
  7.      <script>  
  8.          $(document).ready(function () {   
  9.              $("#Save").click(function () {  
  10.                  $.getJSON('http://localhost:3413/api/person'function (data) {  
  11.                      console.log(data);  
  12.                  });   
  13.              });  
  14.          });  
  15.     </script>  
  16. </head>  
  17. <body>  
  18.         <input type="button" id="Save" value="Save Data" />  
  19. </body>  
  20. </html>  
The getJSON() function takes three parameters (generally) but in this example we are passing an URL and a success callback function as an argument. So, when data is returned it will be held by a callback function and will get logged to the console.
 
Create Web API to return JSON data
 
Now we will create a Web API that will return JSON data. Here is the code example.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;   
  7.   
  8. namespace WebApplication1.WebAPI  
  9. {  
  10.     public class personController : ApiController  
  11.     {  
  12.         [HttpGet]  
  13.         public List<string> Post()  
  14.         {  
  15.             List<string> List = new List<string>();  
  16.             List.Add("Sourav Kayal");  
  17.             List.Add("Ajay Joshi");  
  18.             List.Add("Nontey Banerjee");  
  19.             return List;  
  20.         }  
  21.     }  
  22. }  
In this example we are returning a List of strings but the fact that needs to be explained is that in the return we are not converting to JSON format. This is the beauty of the Web API. At the time of content negotiation the return data is exchanged in JSON format, there is no need to convert it explicitly. Here is the output of the example above.
 
 
Return complex data in JSON format
 
In the above example we have returned a string (primitive type) and in this example we will return complex data, in other words an object. Here is the implementation of a client program that will consume complex data in JSON format.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>   
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6.     <script src="jquery-1.7.1.js" type="text/javascript"></script>  
  7.      <script>  
  8.          $(document).ready(function () {   
  9.              $("#Save").click(function () {  
  10.                  $.getJSON('http://localhost:3413/api/person'function (data) {  
  11.                      console.log(data);  
  12.                  });  
  13.              });  
  14.          });  
  15.     </script>  
  16. </head>  
  17. <body>  
  18.         <input type="button" id="Save" value="Save Data" />  
  19. </body>  
  20. </html>   
The function is very simple, just taking two arguments. The first one is a URL and the second one is a callback function.
 
Create Web API to return complex data
 
In this example we will configure a Model call and we will create a set of objects to that class. The function will return data in JSON format.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;   
  7.   
  8. namespace WebApplication1.WebAPI  
  9. {  
  10.     public class person  
  11.     {  
  12.         public string name { getset; }  
  13.         public string surname { getset; }  
  14.     }  
  15.     public class personController : ApiController  
  16.     {  
  17.         [HttpGet]  
  18.         public List<person> Post()  
  19.         {  
  20.             List<person> List = new List<person>();  
  21.             person p1 = new person();  
  22.             p1.name = "Sourav";  
  23.             p1.surname = "Kayal";  
  24.             List.Add(p1);             
  25.   
  26.             person p2 = new person();  
  27.             p2.name = "Sourav";  
  28.             p2.surname = "Kayal";  
  29.             List.Add(p2);  
  30.             return List;  
  31.         }   
  32.     }  
  33. }  
We have implemented a Post() method to return data, anyway we can use a Get() method also to return JSON data. Here is the output of the example above.
 
 
Conclusion
 
In this article we have learned how to use the getJSON() function to get JSON data from a Web API RESTful service. I hope you have understood the concept of the getJSON() function.