Googlemaps Api v3 getJson无法在IE中运行

时间:2013-02-23 18:23:10

标签: api internet-explorer jquery

我有以下问题:我试图通过ajax从googlemaps api v3获取数据,代码如下。它适用于Chrome和Firefox,但不适用于IE。我做错了什么?

var lat;
$.ajax({
url: "url",
cache: false,
dataType: "json",
success: function(data) {      
lat = data.results[0].geometry.location.lat;
}                                                                                                   
});

1 个答案:

答案 0 :(得分:0)

将以下错误事件添加到Ajax调用

  success: function(data) {
     lat = data.results[0].geometry.location.lat;
    },
   error: function(xhr, status, error) {
     alert(status);
   }

让我们知道您在IE中收到的错误消息,如果有的话。

编辑 - 听起来您正在尝试进行跨域请求? 如果是这种情况,请尝试在ajax请求之前设置$.support.cors = true;

http://api.jquery.com/jQuery.support/

获取
  

要在不支持cors的环境中启用跨域请求,但是   允许跨域XHR请求(Windows小工具等),设置   $.support.cors = true;

EDIT2 - 我假设您正在运行IE9,在这种情况下,您需要一个jQuery插件: https://github.com/jaubourg/ajaxHooks/blob/master/src/xdr.js

另请参阅SO上的这个答案,了解有关为什么IE9需要插件的详细信息(有cors):IE9 jQuery AJAX with CORS returns "Access is denied"

EDIT3 - 下载jQuery.XDomainRequest.js并在您的AJAX通话之前加入它。