You are in the "Web API with AJAX" article series. In our previous articles we have understood various concepts and the practical implementation of AJAX requests to the Web API. We have learned the concept of various HTTP verbs and the consumption of a Web API service using those verbs using the ajax() jQuery function. You can go through all of them by navigating to the following links.
- Web API with AJAX: Understand POST request in Web API
- Web API with AJAX: Understand GET request in Web API
- Web API with AJAX: Make PUT Request in RESTful Web API Service
- Web API With AJAX: Understand DELETE Verb in Restful Web API
- Web API With AJAX: Use GetJSON() Function to Get JSON Data
- Web API with AJAX: Understand Method Name and Attribute in Web API
- Web API with AJAX: Understand FormBody and FormUri attribute inWeb API
- Web API With AJAX: Understand AcceptVerb Attribute in Web API
- Web API With AJAX: Various Parameters of jQuery Ajax() Function
- Web API with AJAX: Perform Cross-Domain AJAX Request using POST Verb
- Web API With AJAX: Cross Domain AJAX Call With GET Request
In this article we will see the real beauty of the Web API, Ha..Ha.., Yes the Web API is quite beautiful without make-up (read configuration) than other services like WCF and a traditional WebService. That's nice, but the real power of the Web API is that it's content type negotiation. In other words, it can serve any type (Oh, not any type but in a different flavor) content to the client end depending on the demand of the client and that's the demand of various electronic gadgets. Now a days people are using various portable devices to browse web applications and their demanded content type is also different and the Web API is smart enough to fulfill their demands. How? Please continue your job (reading).
Return JSON data from Web API
In this example we will learn to return JSON data from the Web API. Nothing to learn here, by default the Web API returns JSON data if the client does not specify any demand type. Have a look a the Web API part, we will not configure anything (in another example also) and we will tune only the client part. Here is a sample example.
Implementation of Web API
This is the Web API implementation. From the Get() method we are returning a collection of person classes. Pretty simple to implement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TestWEB_API.Models;
namespace TestWEB_API.Controllers
{
public class person
{
public string name { get; set; }
public string surname { get; set; }
}
public class ValuesController : ApiController
{
// GET api/values
public List<person> Get()
{
List<person> List = new List<person>();
person p1 = new person();
p1.name = "Sourav";
p1.surname = "Kayal";
List.Add(p1);
person p2 = new person();
p2.name = "Sourav";
p2.surname = "Kayal";
List.Add(p2);
return List;
}
}
}
Implementation of client part
In the jQuery ajax() function we are not providing any content type or data type. So by default it will return JSON data as expected.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestWEB_API.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>
<script>
$(document).ready(function () {
$("#click").click(function () {
$.ajax({
url: 'http://localhost:11129/api/values',
type: 'GET',
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log(textStatus);
}
}).done(function () {
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="button" id="click" value="click me" />
</form>
</body>
</html>
Oh yes. It's returning JSON data from the Web API. The data is in the form of a JavaScript object.
Return XML data from Web API
Now in this example we will demand a XML representation of the data to the Web API. And again, for that we will not perform any extra make up for the Web API. Here is the code implementation.
The client part is here that will call the Web API. And we are seeing that we have specified the dataType is "xml" and the contentType is "application/rss+xml"; in other words it's demanding XML data to the service application.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestWEB_API.WebForm1" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" src="Scripts/jquery-1.7.1.min.js"></script>
<script>
$(document).ready(function () {
$("#click").click(function () {
$.ajax({
url: 'http://localhost:11129/api/values',
type: 'GET',
dataType: 'xml',



hari omPosted Mar 16, 2018, 5:04 AM
As per my understanding By defualt web api returns xml data, and ajax return json type data type. for example if we do not consume web api with any client and run simple web api on browser as a application we can see get method by deafult returns xml data. and if we want to change datatype json than we can change and result will get in json.
Tasneem PPosted Oct 1, 2015, 3:36 AM
I want to know how to get post methods's return value to know succes or failure
Jaganathan BantheswaranPosted Jan 10, 2014, 2:35 AM
Is there any update?
Jaganathan BantheswaranPosted Dec 16, 2013, 11:42 PM
I may suggest you to split this article into two parts. 1. Web API With AJAX: Return Various Data Types From Web API Service[ return different types from Web API]. 2. Web API With AJAX: Handling Different Data Types in AJAX with Web API [handles the response [using dataType in ajax call] whatever returned from API].
Sourav KayalPosted Dec 16, 2013, 10:04 AM
dear Jaganathan, can you suggest any relevant title, then it will help me to take farther step.
Jaganathan BantheswaranPosted Dec 16, 2013, 4:29 AM
Is there any update or my comment is wrong?
Pramod LawatePosted Dec 13, 2013, 12:12 PM
excellent ......superb.......article keep updating us .
Jaganathan BantheswaranPosted Dec 13, 2013, 10:31 AM
yes Sourav, i read your content completely But your title & content is misleading the users. Conversion to specified data type like JSON, XML, HTML, TEXT, SCRIPT is done at CLIENT side NOT at Server Side [Web API] in your examlpe. So there is nothing with the WebAPI to data type conversion here in your content. Please change the Title as well as the content
Sourav KayalPosted Dec 12, 2013, 9:50 AM
dear Jaganathan, from client part we are requesting data type, please read the full content.
Preet SinghPosted Dec 12, 2013, 2:51 AM
very nice article... :)
Jaganathan BantheswaranPosted Dec 12, 2013, 1:04 AM
I am confused. Whether the Web API is returning the different types of data or jQuery ajax parses the result in different types as specified in dataType property of ajax call ?