javascript - Dart JSONP callback error -
i trying receive data using jsonp. when hit https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q=barack%20obama&callback=callbackforjsonpapi
i response fine:
callbackforjsonpapi({response here})
but when go https://api.forecast.io/forecast/apikeyhere/37.8267,-122.423?callback=callbackforjsonapi get:
typeof callbackforjsonapi === 'function' && callbackforjsonapi({ response here}) can explain why have "typeof" part prepended 1 response not other?
this site.dart file:
void main() { // listen postmessage main page window.onmessage.listen(datareceived); scriptelement script = new element.tag("script"); script.src = "https://api.forecast.io/forecast/apikey/37.8267,-122.423?callback=callbackforjsonapi"; document.body.children.add(script); } datareceived(messageevent datareceived) { var data = json.parse(datareceived.data); print(data['responsedata']); } this partial html:
<html> <body> <script type="text/javascript"> function callbackforjsonpapi(s) { var data = json.stringify(s); window.postmessage(data, '*'); } </body> </html>
i can't explain why these different; seems bit of oversight on google's part (presumably should same).
both of them valid; 1 check looks best; it's trying call function if exists (and function); stop javascript error if haven't declared function.
if you're pulling in server-side , spitting code page; should try , make code tolerate both of these; in case happen different behaviour in future (eg. if google make these consistent).
Comments
Post a Comment