Friday, January 14, 2011

Using JSONP with Grails

This was the scenario; a Grails' controller had to be called using AJAX and then it's result displayed on a PHP page. I knew that jQuery supported JSONP but had no idea how to render a JSONP object in Grails. After hours of frustration and googling around, I finally configured it out. A small sample describing it follows.

Sample Client Program:
$(function(){
$('#searchForm').submit(function(){
var url = 'http://someremotesite.com/search/index?q=test&id=10&callback=?';
$.getJSON(url, null, function(data) {
alert('Result = '+data['result']);
});
return false;
});
});
The most important part is the string "callback=?" added to the end of the URL(see line 3 of program above) makes jQuery to recognise it as a JSONP call. What it basically does is that it supplies the name of the default handler for the AJAX call by storing it in the variable "callback" which we use on the server side.

Sample Server Program:
import grails.converters.JSON

class SearchController {

def index = {

//do some thing

def result = someCalculation()

render "${params.callback}(${result as JSON})"
}
}
The last line(line 11) prints the default assigned callback function along with the JSON data as its parameter. For example, if the default callback function assigned by jQuery is "handleOutput" and jsonData is the "result" object rendered as JSON in above example then Grails renders the output as: handleOutput(jsonData).

The returned JSON object can be manipulated in the same manner as we manipulate any other JSON objects using javascript.

Hope this helps someone out there.

Keep on coding.

Thursday, December 23, 2010

Cache Flash Videos (flvs) from youtube like sites

In linux, when we are viewing the flash videos in our browser (like from youtube, facebook etc), the cache of the file is usually saved in /tmp folder. When you close the browser with the video, the cached file is also removed.

There are some times when I cache those videos but forget to view it. The whole cache is gone and the next time, the video needs to be buffered again. So, this simple bash script saves the flash videos each time they are updated.


#!/bin/sh
while true
do
if [ `inotifywait -e modify /tmp 2> /dev/null| wc -l` -gt "0" ]; then
cp -R /tmp/Flash* /home/anjan/temp
fi
done;


If you don’t have inotify-tools installed, install it using

sudo apt-get install inotify-tools

Wednesday, December 22, 2010

Configuring simple JSONP server and client

I am working in a project which is modular. The modules run in different servers. I was planning to get the list of departments from one module to the other using JSON (because it’s cool). However, for me, the limitation in working with JSON was “Same Origin Policy”. I couldn’t request for the JSON data from one of my modules to the other module running in different server (different protocol, port or the IP).
The solution to the problem was using JSONP instead. We create a function in the server which will process requests from the client and sends back not only the JSON data, but also the callback function that the client must call. This way, when we request to the server using
tag, the function created in our client side is called with the callback and we can use the JSON data according to the format.
<script type="text/javascript">

Quick Server in PHP

<?php
header('Content-type: application/json;charset=UTF-8');
$jsonData;
$type = $_GET['parentDept'];
if($type == 'OPD'){
$jsonData = '[
{"deptId": "3", "label":"OPD:Eye"},
{"deptId": "4", "label":"OPD:Ortho"}
]';
}
else{
$jsonData = '[
{"deptId": "1", "label":"Laboratory"},
{"deptId": "2", "label":"Imaging"}
]';
}
echo $_GET['callback'] . '(' . $jsonData . ');';
?>


server finds the callback parameter value and appends it to be returned as the function with the JSON data into it. Also note, we can add up other parameters (eg. parentDept) if needed.

Quick Client


<html>
<head>
<title>JSONP test</title>
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="json.min.js"></script>
</head>
<body>
<script type="text/javascript">
$(document).ready(function(){
$.getJSON("http://192.168.1.10/jsonp.php?parentDept=OPD&callback=?", function(data){
//alert(JSON.stringify(data));
var htmlString = '';
$.each(data, function(index, value){
htmlString += '<option value="' +value.deptId+'">'+ value.label +'</option>';
});
$("#selectField").html(htmlString)
});
});
</script>
Select Department:
<select id="selectField">
<option>None</option>
</select>
</body>
</html>

Here JQuery’s $.getJSON() is used which makes it easier for us to make JSONP call and process the callback function.
You can download the zip content of this simple project here.

Reference: Cross-domain communications with JSONP (ibm.com)

Monday, May 25, 2009

Deerwalk

This is the first blog by me on Deerwalk.

Let me share few things about it...

Deerwalk Inc. is founded by experienced software executives with more than 30 years of experience in developing and managing Enterprise Software. We are based in Massachusetts and have a large development center in Nepal. We can deliver the cost benefits of offshore development with the convenience of working with a US based team.

We offer full product development lifecycle services to complete your projects on time, under budget, and above expectations. For further information, please visit us at www.deerwalk.com

Deerwalk Inc.
9 Meriam St, Suite 21
Lexington, MA 02420
United States
Phone: (781) 325-1775
Fax: 781-498-2785

Deerwalk Nepal Pvt. Ltd
111 BishalBasti, Ka Marga
Bishal Nagar, Kathmandu, Nepal
Phone: 977-1-4417941