2012-10-16 28 views
2

的的getJSON()調用我正在使用新的 Google Time Zone API錯誤在新的谷歌時區API

我用它來知道在地圖的緯度/長點的準確時間。

首先,我讀到Google在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提供響應。

有沒有人知道我怎麼可以做一個GET請求的https沒有跨域問題?

謝謝你!

回答

2

Does anyone know how I can make a GET request for https without the problem of cross domain?

這是一個Web服務,從您的服務器(或在您的服務器上使用代理)檢索它。

+0

所以我將無法從Javascript查詢? 隨着Geonames服務,我沒有任何問題,因爲它是http,而不是https。 所以看來問題是該網址是https類型?因爲使用http URL,如果有效的話。謝謝! – Adagio

+0

最後我打電話從一個PHP。從Javascript是不可能的。 – Adagio

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 
    } 
} 
+0

看來,這是一種在JSONP中轉換響應的方法?謝謝! – Adagio

+0

是的,還有一種方法可以解決導致錯誤信息的「相同原點策略」 –