RESTful Ajax in ColdFusion


While I was hacking and cursing my way through a routine to convert RSS feeds in to HTML … I had an idea. I thought, “Wow, maybe there’s another way to do this …” Converting RSS can be messy so I opted for a much easier and cleaner solution … JSON. For those Ajax pros out there … have a heart … I’m definitely a nOOb at JSON … I just wanted to share my experience because the introduction and experience was enlightening … at least to me it was.

Before I go in to this, I am offering this obligatory warning. What I’m about to do may not be in compliance with Google’s T.O.S. So, if you get yourself in a pickle with the big G … It’s on you. You’ve been warned.

What I wanted to do is create dynamic and fresh content for say … Oh the news. That’s a no brainer. Typically you could just grab some RSS feeds and embed them in your content. So, What if you wanted the HTML from say … Google’s news … Hmmm? You could use a SOAP request to Google for the info … But SOAP won’t be supported by Google for much longer … So, that’s no good. That’s where Google’s RESTful JSON interface comes in. According to Google documentation for their AJAX Search API, The interface was created for developers that need to have access to Google’s Search API in non-JavaScript environments. The docs provide the base URL’s to retrieve results for several of their searchers. Here’ s a list of all the types of searchers you can access remotely.

  • Web Search:
  • Local Search:
  • Video Search:
  • Blog Search:
  • News Search:
  • Book Search:
  • Image Search:
  • Patent Search:

So I decided to have my hand at grabbing some news … Here we go …

<!--- Create a Couple Vars for the Search Params ... --->
<cfset gQuery = #ReReplaceNoCase("ColdFusion","\s+","%20","ALL")#>
<cfset qRegion = #ReReplaceNoCase("Fort Lauderdale","\s+","%20","ALL")#>
<!--- Call Google's AJAX Service --->
<cfset gData = "">

<!--- Save the Result to a Var --->

<!--- Make the Data is JSON --->
<cfset gData = #SerializeJSON(gDataResult,false)#>

<!--- Clean up the Result Data with RegEx --->
<cfset gData = #REReplace(gDataResult.FileContent,
<cfset gData = #REReplace(gData, "\s*\)\s*$", "")#>

<!--- Make Sure We Have JSON ... --->
<cfif !IsJSON(gData)>
<h3>Uh Oh ... Somthing Went Terribly Wrong ... <br />
But! Don't Fret ... Coders are Hard at Work to Get Things Up ... <br />
Right Away ...

<!--- If the JSON is Good, Deserialize It. --->
<cfset gData = #DeserializeJSON(gData)#>

<!--- Create a Var for the Reponse Data&nbsp;&nbsp;&nbsp;&nbsp;--->
<cfset response = #gData.responseData.results#>
<cfloop from="1" to="#ArrayLen(response)#" index="ndx">
<a href="#response[ndx].unescapedurl#" rel="nofollow"
</strong><br /><br/>
#response[ndx].content# <br /> <br />

The Google Search API’s Class reference has a list of all the arguments you can include as URL parameters that will give you plenty of options to customize your results …

The following two tabs change content below.
Edward J. Beckett is a passionate software engineer, web developer, server administrator and polyglot programmer with nearly a decade experience building desktop and web applications ranging from simple personal web sites to enterprise level applications on many technology stacks including Java, Java EE, Spring, Spring MVC, Spring Data, Hibernate, SQL, JPA, JMS, HTML, CSS, JavaScript, ColdFusion, PHP, Node.js and more...