【问题标题】:Error in the getJSON() call of the new Google Time Zone API新的 Google Time Zone API 的 getJSON() 调用出错
【发布时间】:2012-10-16 11:52:22
【问题描述】:

我正在使用新的 Google Time Zone API

我用它来知道地图的纬度/经度点的确切时间。

首先,我了解到谷歌在 6 天前(2012 年 10 月 10 日)推出了这个 api,所以我没有找到关于这个 API 调用的文档。

我有以下问题:

Google 强制你使用 https,我得到了跨域错误:

XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/timezone/json?location=35.209722,-118.300781&timestamp=1350372338&sensor=false Origin http://my_web is not allowed by Access-Control-Allow-Origin.

这是我获取 JSON 的代码:

url = "https://maps.googleapis.com/maps/api/timezone/json?location=35.209722,-118.300781&timestamp=1350372338&sensor=false";  
$.ajax({  
                 url: url,  
                 //data: {},  
                 type: "GET",  
                 crossDomain: true,  
                 dataType: 'json',  
                 //processData: false,  

                 success: function (data) {  
                     console.log('Succes!! Read some data...');  
                 },  
                 error: function (xhr, err) {  
                     console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);  
                     console.log("responseText: " + xhr.responseText);  
                 }  
             });

在许多帖子中推荐使用 JSONP 而不是 JSON,但问题是 Google 只提供 JSON 和 XML 的响应。

有谁知道如何在不存在跨域问题的情况下对 https 发出 GET 请求?

谢谢!

【问题讨论】:

    标签: javascript google-maps google-geocoding-api


    【解决方案1】:

    有谁知道如何在不存在跨域问题的情况下对 https 发出 GET 请求?

    这是一项网络服务,请从您的服务器中检索它(或使用您服务器上的代理)。

    【讨论】:

    • 所以我将无法从 Javascript 查询?使用 Geonames 服务我没有问题,因为它是 http,而不是 https。所以看起来问题是URL是https类型的?因为如果可行的话,使用 http URL。谢谢!
    • 最后我从一个 php.ini 调用。从 Javascript 是不可能的。
    【解决方案2】:

    您也许可以使用YQL console 使其正常工作。您可以将它与 json 调用一起使用以检索 json 数据。

    你需要在控制台里面放一些类似

    的东西
    select * from html where url="https://maps.googleapis.com/maps/api/timezone/json?location=35.209722,-118.300781&timestamp=1350372338&sensor=false"
    

    你可以看到结果。下面是一些示例伪代码。您需要自己找出可以用于 xpath 的内容(如果需要)

    var url = 'https://maps.googleapis.com/maps/api/timezone/json?location=35.209722,-118.300781&timestamp=1350372338&sensor=false';
    var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + url + '" AND xpath="....."') + '&format=json&callback=?';  
    $.getJSON(yql,function(data){  
        if(data.results){
         ... do your thing here with the data
        }
    }
    

    【讨论】:

    • 看来这是在JSONP中转换响应的一种方式?谢谢!
    • 是的,还有一种方法可以克服导致您的错误消息的same origin policy
    猜你喜欢
    • 2013-04-12
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    • 2011-03-14
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多