Amazing super casino bonuses await online gambling enthusiasts who register with Casino770, joint hem and win your fortune too!
The Ashes
Bing API does callback checking for JSON-P
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:
-
if(typeof callback == ‘function’) callback(
-
{
-
“SearchResponse”:
-
{
-
“Version”:“2.2″,
-
“Query”:
-
{
-
“SearchTerms”:“a hard day’s night”
-
},
-
“Translation”:
-
{
-
“Results”:
-
[
-
{“TranslatedTerm”:“einem harten Tag-Nacht “}
-
]
-
}
-
}
-
} /* 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:
-
if(typeof callback === ‘function’) {
-
callback(… data … );
-
} else {
-
if (typeof console!==‘undefined’ &&
-
typeof console.log !== ‘undefined’){
-
console.log(‘Error: Callback method not defined’);
-
}
-
}
More details about other ideas to improve this are here.
No Comments