Calling ASP.NET Web API Service In Same Domain Using AJAX

Introduction

This article explains how to call the ASP.NET Web API in same domain using AJAX in asimple and step by step way. We can deploy and use our Web API in the same domain as well as cross-domain. 

Calling Web API in same domain

Web page and Web API should be hosted in the same domain or same origin. For example, if a web application is hosted on “http://test.com”, then, the Web API should also be hosted on “http://test.com”



Steps for creating same domain

Steps 1

Open a new application and add Web API Solution. After opening ASP.NET Web API solution, add new Web API Controller.


Now, add a class in a Models folder for accessing data from Web API Controller to the web page. The added class and its properties look like the below code. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace WebAPIsameDomain.Models  
  7. {  
  8.     public class Customer  
  9.     {  
  10.         public int Id { set; get; }  
  11.         public string CustomerName { set; get; }  
  12.         public string Address { set; get; }  
  13.         public string City { set; get; }  
  14.         public string Pincode { set; get; }  
  15.     }  
  16. }   

Step 2

Assign customer details in customer list variable and add in “DemoController”.  

  1. namespace WebAPIsameDomain.Controllers  
  2. {  
  3.     public class DemoController : ApiController  
  4.     {  
  5.         public IEnumerable<Customer> Get()  
  6.         {  
  7.             return new List<Customer>  
  8.             {  
  9.                 new Customer{Id=1,CustomerName="Arun",Address="9th Street",City="Erode",Pincode="637204"},  
  10.                 new Customer{Id=2,CustomerName="Mohan",Address="Main Road",City="Namakkal",Pincode="637205"},  
  11.                 new Customer{Id=3,CustomerName="Prapbu",Address="SKV Street",City="Salem",Pincode="637206"},  
  12.                 new Customer{Id=4,CustomerName="Raja",Address="Raja Street",City="Chennai",Pincode="637207"},  
  13.                 new Customer{Id=5,CustomerName="Ravi",Address="SMV Street",City="Covai",Pincode="637208"},  
  14.                 new Customer{Id=6,CustomerName="Santhose",Address="SKM Street",City="Salem",Pincode="637209"},  
  15.                 new Customer{Id=7,CustomerName="Mugesh",Address="Main Road",City="Trichy",Pincode="637846"},  
  16.                 new Customer{Id=8,CustomerName="Mani",Address="Mani Street",City="Chennai",Pincode="637245"},  
  17.                 new Customer{Id=9,CustomerName="Sankar",Address="Sankar Street",City="Tiruchengode",Pincode="637273"},  
  18.                 new Customer{Id=10,CustomerName="Vignesh",Address="Main Road",City="Kondankattur",Pincode="637201"}  
  19.             };  
  20.         }  
  21.     }  
  22. }   

Step 3

Add new HTML page in your solution and add AJAX code for calling ASP.NET Web API service in HTML page. For calling Web Service in web page, add the below code in HTML page. 

  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.     <script src="//code.jquery.com/jquery-1.12.3.js"></script>  
  6.     <script>  
  7.         $(document).ready(function () {  
  8.             var ulCustomers = $("#Customer");  
  9.   
  10.             $("#GetallData").click(function () {  
  11.                 $.ajax({  
  12.                     type : 'GET',  
  13.                     url: 'http://localhost:49352/api/Demo',  
  14.                     dataType: 'json',  
  15.                     success: function (data) {  
  16.                         ulCustomers.empty();  
  17.                         $.each(data, function (index,val) {  
  18.                             var name = val.CustomerName;  
  19.                             ulCustomers.append('<li>' + name + '</li>');  
  20.                         });  
  21.                     }  
  22.                 });  
  23.             });  
  24.         });  
  25.     </script>  
  26. </head>  
  27. <body>  
  28.     <input type="button" id="GetallData" value="Get All Data" />  
  29.     <ul id="Customer"></ul>  
  30. </body>  
  31. </html>   

Step 4

Now, build ASP.NET Web API and run the corresponding Controller. Check the Web API URL and results.


IN the same way, run the HTML page. You can see the output and URL of HTML page that looks like the below screenshot.


Now, we can see the origin of the ASP.NET API Service and web page by URL. Both start from same origin http://localhost:49352/. 

http://localhost:49352/api/Demo - ASP.NET Web API service

http://localhost:49352/Customers.html - Web Page

In our Web page, we gave full URL path for calling the Web Service using AJAX.



We have no need to give full URL if both are in the same domain or origin, we can use a relative path for calling the service.


Step 5

Now, build and run the solution. When we click the button, we will see all customers' names there.


Conclusion

This article explained about calling ASP.NET Web API in the same origin using AJAX in step by step way. The next article will explain how to call ASP.NET Web API in cross-origin, using AJAX.


Similar Articles