Click here to Skip to main content
13,288,869 members (57,867 online)
Click here to Skip to main content
Add your own
alternative version


10 bookmarked
Posted 18 Jul 2010

Calling a WCF Data Service From jQuery

, 18 Jul 2010
Rate this:
Please Sign up or sign in to vote.
I want to show is how easy and simple it is to consume a WCF Data Service (OData feed) with the jQuery library.


I'm working on a lecture about OData which I will present next month (stay tuned for more details in the near future). One of the things that I want to show is how easy and simple it is to consume a WCF Data Service (OData feed) with the jQuery library.
In this post, I'll show you exactly how to do that.

jQuery’s getJSON Method

When you want to load JSON data from the server using a GET HTTP request in jQuery, you will probably use the getJSON method. That method gets as input the server URL, a map of key-value pairs which holds parameters to the server and a callback function to use when the response arrives back to the client and returns the server response in JSON format. This method is the best candidate for consuming WCF Data Services endpoints. If I want to use the getJSON method, I'll do something like the example provided in the getJSON documentation:

$.getJSON('ajax/test.json', function(data) {
  $('.result').html('<p>' + + '</p>'
    + '<p>' + data.baz[1] + '</p>');

How to Consume WCF Data Service with jQuery getJSON Method

When we understand that WCF Data Service is just an HTTP endpoint which returns JSON answer if addressed with JSON request, the road is very clear. If I want to consume the service, all I need to do is create the relevant URI with the WCF Data Service URI conventions and that is it. The following code is an example for that:

$.getJSON("/Services/SchoolService.svc/Courses", null,
    function (data) {
        var sb = new Sys.StringBuilder();
        $.each(data.d, function (i, item) {
            CreateDivForOneCourse(sb, item)

What this code is doing is it goes to a WCF Data Service endpoint that is called SchoolService.svc and retrieves the courses entity set it holds. I don’t pass any parameters to the request so the key-value pair is null. In the callback function, I create a StringBuilder object and append to it the response by iterating the data and creating a div for every course. In the end, I put the result as HTML in a div which has an id of divResults.

The All Example Page

<%@ Page Language="C#" AutoEventWireup="true" 


    	Inherits="DataServiceAjaxClient.ConsumeDataService.ConsumeDataServicejQuery" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<html xmlns="">
<head runat="server">
    <script src="../Content/Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../Content/Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript">
    <script type="text/javascript">                
        var ajaxRequest;
        $(document).ready(function () {                        
            $('#btnShowCourses').click(function () {
        function GetCoursesData() {
            if (ajaxRequest != null) {
            ajaxRequest = $.getJSON("/Services/SchoolService.svc/Courses", null,
            function (data) {
                var sb = new Sys.StringBuilder();
                $.each(data.d, function (i, item) {
                    CreateDivForOneCourse(sb, item)
        function CreateDivForOneCourse(stringBuilder, item) {
            stringBuilder.append("</span> <span>");
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <input type="button" id="btnShowCourses" value="Show Courses" /><br />
        <div id="divResults" />

After running this example, the result looks like:

getJSON result


Consuming WCF Data Services with jQuery is a very easy task. By using the getJSON method, we create the HTTP GET request to the service and get the returning data in JSON format to
our disposal.

</img />
</img />


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...


Comments and Discussions

Questionwindows authentication Pin
pepepaco3-Sep-10 8:29
memberpepepaco3-Sep-10 8:29 
im trying to run this example using my own data service,

but im getting HTTP Error 401.2 Unauthorized

I wonder how can I declare my credentials using pure client side code.


I know that using code behind I can use something like
_entities.Credentials = System.Net.CredentialCache.DefaultCredentials;

but i dont want to use serverside.

AnswerRe: windows authentication Pin
Gil Fink4-Sep-10 20:59
memberGil Fink4-Sep-10 20:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 18 Jul 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid