2011-05-30 36 views
3

我一直無法使用HTTP請求查詢Google Fusion Tables。如果我將以下查詢中的網址粘貼到瀏覽器中,則會返回一個以逗號分隔的列表。但是,當我使用下面的.get函數執行此操作時,數據參數中沒有任何內容。從Google Fusion表中獲取請求時遇到問題

我對此很新,所以任何幫助將不勝感激。

function query(){ 
var jqxhr=$.get(
    "https://www.google.com/fusiontables/api/query?sql=SELECT+Address+FROM+914142+WHERE+IsCustomer+%3D+1", 
    function success(data, textStatus){ 
alert(data);})} 

回答

0

由於Same Origin Policy的原因,您可能無法訪問Google融合表。

一些解決這個方法是代理通過自己的服務器(在同一個域中你所服務的頁面上運行)或JSONP請求數據的請求。

如果您將參數jsonCallback=<callback name here>附加到您的Fusion Tables請求,然後您將得到一個JSONP響應。例如,要求:在

https://www.google.com/fusiontables/api/query?sql=SELECT+Address+FROM+914142+WHERE+IsCustomer+%3D+1&jsonCallback=foo 

結果:

foo({"table":{"cols":["Address"],"rows":[["3400 California Street, Suite 302, San Francisco, CA 94118"],["1200 Pacific Avenue, San Francisco, CA 94109"],["340 10TH Street, San Francisco, CA 94103"],["One Embarcadero Center, Lobby Level, San Francisco, CA 94111"],["2230 Third Street, San Francisco, CA 94107"],["490 Post St, Suite 430, San Francisco, CA 94102"],["530 Bush St. Suite 101, San Francisco, CA 94108"],["114 Sansome Street, Suite 715, San Francisco, CA 94104"],["3012 Steiner Street Suite A, San Francisco, CA 94123"],["199 Fremont St # 105, San Francisco, CA 94105"],["2007 Irving St., San Francisco, CA 94122"],["450 Sutter Suite 2518, San Francisco, CA 94108"],["275 Gough Street, San Francisco, CA 94102"],["450 Sutter Street Suite 1225, San Francisco, CA 94108"],["2675 Geary Blvd., Ste 400, San Francisco, CA 94118"],["332 Pine St # 505, San Francisco, CA 94104"]]}}) 

這從IBM文章應該幫助你理解JSONP以及如何使用它:http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

0

使用JSONP和融合像這個:

function processData(json){ 
    for (var i, row; row=json.table.rows[i]; i++){ 
    console.log(row) 
    } 
} 

script = document.createElement("SCRIPT") 
script.src = "https://www.google.com/fusiontables/api/query?sql=SELECT+Address+FROM+914142+WHERE+IsCustomer+%3D+1&jsonCallback=processData"; 
document.getElementsByTagName("HEAD")[0].appendChild(script); 

沒有測試,你會明白嗎?不知道你的lib是否支持任何有用的抽象。

0

當我想到這一點時,我意識到通過自己的服務器運行請求的最簡單方法是從字面上假裝它是您自己的請求,即存儲在文件中。

所以我剛剛創建一個PHP腳本,包括我自己的域名

<?php echo file_get_contents('http://www.sameoriginpolicydomain.com'); ?> 

內容而這奏效了,無論是從AJAX或直接調用它。下面是你在找什麼:

<?php echo file_get_contents('http://www.google.com/fusiontables/exporttable?query='.urlencode($_GET['query']).'&o=kmllink&g='.$_GET['g']); ?> 
6

我用這個就在今天下午的如何處理數據融合表的查詢posted sample code and a working example掙扎了一會兒回來,和。簡而言之,Mark對於相同的原產地策略(http://en.wikipedia.org/wiki/Same_origin_policy)完全正確,除了一個細節之外,他的解決方案就在那裏 - 您需要用$ .get指定「jsonp」數據類型。請閱讀jQuery .get page

根據您的原來的例子,這應該工作:

function query(){ 
    var queryurl = "<your query url>"; 
    querytail = "&jsonCallback=?"; 

    var jqxhr=$.get(queryurl + querytail, queryHandler, "jsonp") 
} 

function queryHandler(data) { 
    // display the first row of retrieved data 
    alert(data.table.rows[0]); 
} 
+0

真棒是不夠的! – steenhulthin 2011-10-12 18:47:00

+1

注意Fusion Tables的[jsonpAPI](http://googleresearch.blogspot.com/2012/06/introducing-new-fusion-tables-api.html)剛剛發佈。 – 2012-06-26 23:22:27

0

雖然noiv11的回答不工作,它不包括授權那些想知道,你不能與JSONP管理的標頭。爲此,您將需要使用真正的頭文件來檢索數據,因此這需要使用cURL進行一些工作,因此需要通過服務器完成。

下面是一個很有用的PHP類:竅門:Fusion Tables Client PHP