In my last post, I described a Javascript client to display results from Google's JSON search service. In that, I used a PHP proxy to get around Javascript's Same Origin Policy. A cleaner remoting architecture called JSONP or Padded JSON, proposed by Bob Ippolito, and supported by most JSON web services, relies on the server being able to emit a JSON response wrapped in a client specified callback function.
To request padded JSON, the client would populate an optional query parameter which would contain the Javascript callback function name. The client code would implement the callback function. The implementation would typically parse the JSON response and construct HTML to populate into the innerHTML element of a div tag on the page displayed on the browser.
So when the query is sent to the server, the JSON response is wrapped inside the specified callback function name. For example, a query to the Yahoo WebSearchService API would look something like this:
1 2 3 | http://search.yahooapis.com/WebSearchService/webSearch?query=foo&\
callback=handleResponse&\
appid=get-your-own-yahoo-id-and-stick-it-in-here
|
And the server will return a JSON response wrapped within the callback, which is executed as a Javascript function call.
1 | handleResponse(json_response_string);
|
So now if we defined a function handleResponse(String), then whatever is in the function will be executed.
I think this approach is quite beautiful (in the Beautiful Code sense) - not only does it exploit the macro expansion feature in interpreted languages in a clever yet intuitive way, it enables true serverless operation by getting around Javascript's Same Origin Policy.
Setting up the client to do dynamic calls is a bit of a pain with this approach though. Since we don't know the search term until its entered, so using plain Javascript involves manipulating the DOM tree to insert the call into a html/head/script element. However, there are a lot of Javascript frameworks around which make light of this work. One such framework is Dojo, which comes with both JSON and UI components.
In this post, I describe a client that I built using Dojo to run against Yahoo's WebSearch API to display search results from my blog. Dojo has a fairly steep learning curve, but it is very well-documented, and the resulting code is very easy to read and maintain. Here is the code (really an HTML page containing Javascript code):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>My Blog Search Widget</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<style type="text/css">
@import http://o.aolcdn.com/dojo/1.0.0/dojo/resources/dojo.css;
@import http://o.aolcdn.com/dojo/1.0.0/dijit/themes/tundra/tundra.css;
</style>
<script type="text/javascript"
src="http://o.aolcdn.com/dojo/1.0.0/dojo/dojo.xd.js"
djConfig="parseOnLoad: true"></script>
<script type="text/javascript">
dojo.require("dijit.form.Button");
dojo.require("dojo.io.script");
</script>
<script type="text/javascript">
function handleResponse(data, ioArgs) {
var html = '<b>Results ' +
data.ResultSet.firstResultPosition +
'-' +
data.ResultSet.totalResultsReturned +
' for term ' +
dojo.byId('q').value +
' of about ' +
data.ResultSet.totalResultsAvailable +
'</b><br/><br/>';
dojo.forEach(data.ResultSet.Result, function(result) {
html += '<b><a href=\"' +
result.Url +
'">' +
result.Title +
'</a></b><br/>' +
result.Summary +
'<br/><b>' +
result.DisplayUrl +
'</b><br/><br/>';
});
dojo.byId("results").innerHTML = html;
}
</script>
</head>
<body class="tundra">
<p>
<b>Enter your query:</b>
<input type="text" id="q" name="q"/>
<button dojoType="dijit.form.Button" id="searchButton">Search!
<script type="dojo/method" event="onClick">
dojo.io.script.get({
url: 'http://search.yahooapis.com/WebSearchService/V1/webSearch',
content: {
appid: 'get-your-own-appid-and-stick-it-in-here',
query: dojo.byId('q').value,
site: 'sujitpal.blogspot.com',
output: 'json',
callback: 'handleResponse'
},
callbackParamName: handleResponse
});
</script>
</button>
</p>
<hr/>
<div id="results"></div>
</body>
</html>
|
And here is the obligatory screenshot:
No comments:
Post a Comment
Comments are moderated to prevent spam.