The Ashes

Technology, Science and other news
June 16, 2009

Bing API does callback checking for JSON-P

Posted by : admin
Filed under : General

I just looked through the API of Microsoft’s new Bing search and found an interesting step in protecting code from throwing errors.

When you provide a JSON output for developers it does make sense to also allow for a callback parameter. That way your code can be used in script nodes without having to use any backend at all. Commonly this is called JSON-P and has been covered here in the long long ago. One of the issues with JSON-P is that when the callback method is not defined it throws an error.

The Bing API is the first instance where I have seen that they worked around that as the output is this:

PLAIN TEXT
JAVASCRIPT:

  1. if(typeof callback == ‘function’) callback(
  2. {
  3.   “SearchResponse”:
  4.   {
  5.     “Version”:“2.2”,
  6.     “Query”:
  7.       {
  8.         “SearchTerms”:“a hard day’s night”
  9.       },
  10.     “Translation”:
  11.       {
  12.         “Results”:
  13.           [
  14.             {“TranslatedTerm”:“einem harten Tag-Nacht “}
  15.           ]
  16.       }
  17.     }
  18. } /* pageview_candidate */);

I have no clue what the /* pageview_candidate */ is about and frown upon omitting the {} of the if statement, but I must say I do like this. One issue is that while end users don’t get annoyed with errors, developers don’t have a clue what happened either as the error is silent. One proposal would be to use a console.log() when there is an error:

PLAIN TEXT
JAVASCRIPT:

  1. if(typeof callback === ‘function’) {
  2.   callback(data);
  3. } else {
  4.   if (typeof console!==‘undefined’ &&
  5.    typeof console.log !== ‘undefined’){
  6.     console.log(‘Error: Callback method not defined’);
  7.   }
  8. }

More details about other ideas to improve this are here.

Tags :

No Comments

(required)
(will not be published) (required)
(opitional)

cciash.com EN ES IT DE PT CZ FR RU
December 2017
M T W T F S S
« Sep    
 123
45678910
11121314151617
18192021222324
25262728293031

Pages

Categories

Resources

There are many online poker site where you can play but at poker.hk you can play the poker games with all the knowledge you need related to the game with the poker school available in both the English and Chinese language.

Super Casino

Now you can bet on any sports and any sporting events from all the comfort from your home. Bet770 allows you to bet on any events and match with in just 3 clicks. They also offers great odds on football betting for every premier and champions league match. Get £50 free in bets when you register.

Bingo770, offering best online bingo games with £7.70 free no deposit Bonus!