2

我正在使用新的 Google Time Zone API

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

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

我有以下问题:

谷歌强迫你使用 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 请求?

谢谢!

4

2 回答 2

2

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

这是一个 Web 服务,从您的服务器中检索它(或在您的服务器上使用代理)。

于 2012-10-16T12:52:10.720 回答
2

您可能可以使用YQL 控制台来实现它。您可以将它与 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
    }
}
于 2012-10-16T13:07:30.403 回答