• Home
  • New Entries
  • Popular Entries
  • Submit a Story
  • About

Serving JSON data the JSONP way with Groovlets ...

If we want to serve JSON data and want it to be cross-domain accessible we can implement JSONP. This means that if we have a Groovlet with JSON output and want users to be able to access it with an AJAX request from the web browser we must implment JSONP. JSONP isn difficult at all, but if we implement it our Groovlet is much more useful, because AJAX requests can be made from the web browser to our Groovlet.

The normal browser security model only allows calls to be made to the same domain as the web page from which the calls are made. One of the solutions to overcome this is the use of the script tag to load data. JSONP uses this method and basically let is the client decide on a bit of text to prepend the JSON data and enclose it in parentheses. This way our JSON data is encapsulated in a Javascript method and is valid to be loaded by the script element!

The following code shows a simple JSON data structure:

0.{ "title" : "Simple JSON Data",
1.  "items" : [
2.    { "source" : "document", "author" : "mrhaki" }
3.    { "source" : "web", "author" : "unknown"}
4.  ]
5.}

If the client decides to use the text jsontest19201 to make it JSONP we get:

0.jsontest19201({ "title" : "Simple JSON Data",
1.  "items" : [
2.    { "source" : "document", "author" : "mrhaki" }
3.    { "source" : "web", "author" : "unknown"}
4.  ]
5.})

Okay, so what do we need to have this in our Groovlet? The request for the Groovlet needs to be extended with a query parameter. The value of this query parameter is the text the user decided on to encapsulate the JSON data in. We will use the query parameter callback or jsonp to get the text and prepend it to the JSON data (notice we use Json-lib to create the JSON data):

00.import net.sf.json.JSONObject
01.
02.response.setContentType(application/json)
03.
04.def jsonOutput = JSONObject.fromObject([title: Simple JSON data])
05.jsonOutput.accumulate(items, [source: wouldocument, author: amrhaki])
06.jsonOutput.accumulate(items, [source: web, author: unknown])
07.
08.// Check query parameters callback or jsonp (just wanted to show off
09.// the Elvis operator - so we have two query parameters)
10.def jsonp = params.callback ?: params.jsonp
11.if (jsonp) print jsonp + (
12.jsonOutput.write(out)
13.if (jsonp) print )

We deploy this Groovlet to our server. For this blog post I have uploaded the Groovlet to Google App Engine. The complete URL is http://mrhakis.appspot.com/jsonpsample.groovy. So if we get this URL without any query parameters we get:
0.{"title":"Simple JSON data","items":[{"source":"document","author":"mrhaki"},{"source":"web","author":"unknown"}]}

Now we get this URL again but append the query parameter callback=jsontest90210 (http://mrhakis.appspot.com/jsonpsample.groovy?callback=jsontest90210) and get the following output:
0.jsontest90210({"title":"Simple JSON data","items":[{"source":"document","author":"mrhaki"},{"source":"web","author":"unknown"}]})

We would have gotten the same result if we used http://mrhakis.appspot.com/jsonpsample.groovy?jsonp=jsontest90210. The good thing is user is can now use for example jQuery is getJSON() method to get the results from our Groovlet from any web page served on any domain.

The following is generated with jQuery.getJSON() and the following code:
view source
print?
0.$(document).ready(function() {
1.  $.getJSON(http://mrhakis.appspot.com/jsonpsample.groovy?callback=?, function(data) {
2.    $.each(data.items, function(i, item) {
3.      $("<p/>").text("json says: " + item.source + " - " + item.author).appendTo("#jsonsampleitems");
4.    });
5.  });
6.});

 Original Source:
http://mrhaki.blogspot.com/2009/08/serving-json-data-jsonp-way-with.html

AddThis Social Bookmark Button

Posted at 09:42:53 am | Permalink | Posted in JSON  

Related Stuff

  • MooV: Using cutting edge Video phones and Software Video Phones - coupling all that with VoIP and empowering the disabled.

  • Moo Telecom: VoIP communications made easy - Ring anyway with the fun and ease of using a normal phone

  • TagR:Mobile Social Network with Real Time Locations Based services, and Ambience Intelligence, VoiP, IM, Skype, Googletalk, Mapping, Flickr, Events, Calendaring, Scheduling, SecondLife Support

  • ClearSMS : ClearSMS is a Web-based application that lets you send bulk SMS messages to your customers, contacts, or just about anyone.

  • Jajah:jah is a VoIP (Voice over IP) provider, founded by Austrians Roman Scharf and Daniel Mattes in 2005[1]. The Jajah headquarters are located in Mountain View, CA, USA, and Luxembourg. Jajah maintains a development centre in Israel.

  • Skype: It’s free to download and free to call other people on Skype. Skype the number one voice over ip software

  • PrivatePhone: a free local phone number with voicemail and messages you can check online or from any phone.

Top Stuff

MessengerFX

e-messenger

ILoveIM

Top 20 Ruby CMS

MSN Web Messenger

eBuddy



About Ajaxlines

Ajaxlines is a project focused on providing its audience with a database of most of Ajax related articles, resources, tutorials and services from around the world.

Its purpose is to showcase the power of Ajax and to act as a portal to the Ajax development community.


Search


Topics

  • .Net (171)
  • Ajax (89)
  • Ajax Games (10)
  • Articles (95)
  • Bookmarking (35)
  • Calendar (21)
  • Chat (45)
  • ColdFusion (3)
  • CSS (79)
  • Email (23)
  • Facebook (84)
  • Flash (19)
  • Google (54)
  • Html (28)
  • Image (11)
  • International Calls & VOIP (7)
  • Java (56)
  • Javascript (271)
  • jQuery (171)
  • JSON (70)
  • Perl (2)
  • PHP (162)
  • Presentation (19)
  • Python (3)
  • Resources (2)
  • RSS (8)
  • Ruby (31)
  • Storage (4)
  • Toolkits (103)
  • Tutorials (224)
  • UI (11)
  • Utilities (174)
  • Web2.0 (18)
  • XmlHttpRequest (28)
  • YUI (13)

© 2006 www.ajaxlines.com. All Rights Reserved. Powered by IRange