Loading Google Maps API V3 asynchronously with dojo.io.script fails silently

做~自己de王妃 提交于 2019-12-13 15:21:23

问题


Smushing Google's asynchronous loading docs into dojo's asynchronous loader yields:

dojo.io.script.get({
  url: 'http://maps.googleapis.com/maps/api/js',
  jsonp: 'callback',
  content: {
    sensor: 'false'
  },
  load: function() {
    console.log('done');
  },
  error: function() {
    console.log('error');
  }
});

Or fiddle with it: http://jsfiddle.net/sKNmS/

The maps JS files are loaded, but the callback is never called. Why?


回答1:


dojo.io.script.get is only usable for JSONP services. Your callback is never called because, as far as I can tell, that URL does not return a JSONP-formatted response, just plain JavaScript.

dojo.io.script.get sets up a callback, which is expected to be fired by the code returned from that URL. Since that URL points to plain ol' JavaScript, the function never gets fired, because the response isn't JSONP.




回答2:


Looks like dojo.io.script.get() doesn't support Google Maps' delayed invocation of the JSONP callback.

I've filled a dojo feature request.




回答3:


There is another way (tested on dojo 1.6):

dojo.io.script.get({
  url: 'http://maps.googleapis.com/maps/api/js'
}).then(function() {
  console.log('done');
});

Since dojo.io.script.get returns Deferred object.



来源:https://stackoverflow.com/questions/7438854/loading-google-maps-api-v3-asynchronously-with-dojo-io-script-fails-silently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!