Yahoo Spelling Service API – Ajax version

Man, the web is great. I needed to add spelling suggestion to our search this morning. I spent about 10 seconds thinking about how to implement it myself, then thought, hmm…I bet someone has a web service for this.

Yup, Yahoo’s service is super-simple. One REST call with the query string, and it returns a single value with the spelling suggestion. At first I implemented it in my server code, so my server was calling Yahoo to get the suggestion.

But that’s crappy, because then my web server sits busy waiting for Yahoo to answer. So I converted my code to do everything as an Ajax call from the page. Is nice! Same result, but the client calls Yahoo directly and I get no load at all.

The code is easy, making use of the “Dynamic Script Tag” approach to allow the cross-domain call. Using Yahoo’s JSON result option makes handling the result incredibly easy.

<div id=”spelling” style=”font-size:1.2em;font-weight:bold”></div>

<script id=”spelling_json_container”></script>
window.onload = function()
var q = “bad splling”;
             V1/spellingSuggestion?appid=YahooDemo&query=’ + q +

function handleYahooSpellingResults(json_result)
  if (json_result.ResultSet && json_result.ResultSet.Result) {
  result = json_result.ResultSet.Result;
  $(‘spelling’).innerHTML =
      ‘Did you mean <a href=”/site/search?q=’ +
      escape(result) + ‘”>’ + result + ‘</a>?<br /><br />’;

I love the web!.

No comments yet

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: