Click here to Skip to main content
Click here to Skip to main content

Making a JSONP Call to a WCF Data Service using datajs

By , 25 Apr 2011
 

Introduction

Making a JSONP Call for a WCF Data Service using datajs

During MIX11, I attended a very interesting session about datajs which was presented by Asad Khan and Marcelo Lopez Ruiz. As I promised in my previous post, in this post, I’ll show you how to make a JSONP call to a WCF Data Service using the datajs library.

A little about datajs

datajs is a very promising JavaScript library which is currently being built by Microsoft as an Open Source in CodePlex. The library as described in its CodePlex site as “a new cross-browser JavaScript library that enables data-centric web applications by leveraging modern protocols such as JSON and OData and HTML5-enabled browser features. It's designed to be small, fast, and easy to use”. Some of its current features are the ability to communicate with OData services and APIs to work with local data through browser features such as Web Storage (and in the future, IndexedDB). For further reading about datajs, go to its documentation, or download the library and play with it (there is also a NuGet package for it).

Making a JSONP call using datajs

Here is the implementation of the same JSONP call functionality from the previous post but using datajs:

<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title>JSONP Call using datajs</title>
    <script src="Scripts/datajs-0.0.3.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" runat="server">
    <output id="result">
    </output>
    </form>
    <script type="text/javascript">       
        OData.defaultHttpClient.enableJsonpCallback = true;
        OData.read("http://localhost:23330/SchoolDataService.svc/Courses", 
            function (data, request) {
                var output = document.getElementById('result');
                for (var i = 0; i < data.results.length; i++) {
                    var div = document.createElement('div');
                    div.innerHTML = data.results[i].Title;
                    output.appendChild(div);
                }
            });               
        
    </script>
</body>
</html>

A few things to notice in the example:

  • I add the script to datajs which exists in my web application project under the Scripts directory.
  • You need to enable JSONP through OData.defaultHttpClient.enableJsonpCallback, which is disabled by default.
  • Use the OData.Read function to make the call to the service (no need to add the format or callback query parameters like I did in the previous post since they are added automatically).

Pay attention that in a real world application, you would probably like to disable JsonpCallback after you make the call.

Summary

datajs is still in Alpha version, and it is only in its starting point. It tries to provide a common solution around the data problem in client-side development. I expect it to grow and to supply more capabilities in the future. In this post, I showed how to use it in order to make a JSONP call to a JSONP enabled WCF Data Service.

License

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

About the Author

Gil Fink
Architect Sela Group
Israel Israel
Member
Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior Architect at SELA Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft.
 

My technical blog: http://www.gilfink.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow about a POST/PUTmemberChristianSM20 Sep '12 - 7:33 
Hi,
I set my WCF service with your JSONPSupportBehavior attribute.
I'm using datajs, too. If I request a GET (using OData.read in your example) it works perfectly.
But if I request a post or a put, the system returns the error: request is not local and cannot be done through JSONP.
Would you please give an example with POST or PUT (OData.request)
 
Thanks!
Christian
Generaladd, update, delete using Odata.request demo requiredmemberMember 7785814 Jun '11 - 7:45 
Hello ,
 
Can you please show us a demo that describes the add, update, delete using OData.request
 
Best regards
Dhinesh Kumar
Dhinesh

GeneralHimembermurtaza dhari18 May '11 - 20:21 
Hi its nice post but can you please upload or email me the WCF part i cant make it work it gives me bad request error
 
Thanks
Ali, Murtaza

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 25 Apr 2011
Article Copyright 2011 by Gil Fink
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid