来自Google Fusion表的Get请求有问题

时间:2022-11-09 22:25:56

I've been having trouble querying the Google Fusion Tables using an HTTP request. If I paste the URL from the query below in the browser, it comes back with a commas separated list. However, when I do this with the .get function as below, nothing comes back in the data parameter.

我一直无法使用HTTP请求查询Google Fusion Tables。如果我将以下查询中的URL粘贴到浏览器中,则会以逗号分隔的列表返回。但是,当我使用.get函数执行此操作时,数据参数中没有任何内容返回。

I'm fairly new at this, so any help would be appreciated.

我对此很新,所以任何帮助都会受到赞赏。

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);})}

5 个解决方案

#1


6  

I was struggling with this a while back, and just this afternoon posted sample code and a working example of how to handle Fusion Tables queries.

我一直在努力争取这一点,就在今天下午发布了示例代码以及如何处理Fusion Tables查询的工作示例。

In a nutshell, Mark is exactly right about the same origin policy (http://en.wikipedia.org/wiki/Same_origin_policy), and was just about there with his solution aside from one detail - you need to specify "jsonp" datatype with $.get. Read on at the jQuery .get page.

简而言之,Mark完全相同的原始政策(http://en.wikipedia.org/wiki/Same_origin_policy),除了一个细节之外,他的解决方案就在那里 - 您需要指定“jsonp”数据类型用$ .get。请继续阅读jQuery .get页面。

Based on your original example, this should work:

根据您的原始示例,这应该工作:

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]);
}

#2


0  

Use JSONP and fusion like this:

像这样使用JSONP和fusion:

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);

Not tested, you'll got the idea? Don't know whether your lib supports any useful abstraction.

没有经过测试,你会明白吗?不知道你的lib是否支持任何有用的抽象。

#3


0  

You likely being prevented from accessing Google fusion tables due to the Same Origin Policy.

由于同源策略,您可能无法访问Google融合表。

Some ways to solve this is to proxy the request through your own server (running on the same domain as the page you are serving) or requesting the data in JSONP.

解决此问题的一些方法是通过您自己的服务器(在与您服务的页面相同的域上运行)或在JSONP中请求数据来代理请求。

If you append the parameter jsonCallback=<callback name here> to your Fusion Tables request, then you will get a JSONP response. For example, the request:

如果将参数jsonCallback = 附加到Fusion Tables请求,那么您将获得JSONP响应。例如,请求:

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

results in:

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"]]}})

This article from IBM should help get you understand JSONP and how to work with it: http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

IBM的这篇文章应该有助于您了解JSONP以及如何使用它:http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

#4


0  

When I thought about this I realised that the easiest way to run the request through your own server would be to literally pretend it was your own request i.e. in a file stored.

当我想到这一点时,我意识到通过你自己的服务器运行请求的最简单方法就是假装它是你自己的请求,即在存储的文件中。

So I just created a php script that included the contents on my own domain

所以我刚创建了一个包含我自己域名内容的php脚本

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

And that did the trick, either calling it from AJAX or directly. Here's what you're looking for:

这就是诀窍,无论是从AJAX还是直接调用它。这是你要找的东西:

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

#5


0  

While noiv11's answer does work, it doesn't cover authorization for those wondering as you can't manage the headers with JSONP. For this you will need to use real headers to retrieve the data so this will require a little work with cURL and therefore need to be done via a server.

虽然noiv11的答案确实有效,但它不包括那些想知道你无法用JSONP管理标题的人的授权。为此,您需要使用真实的标头来检索数据,因此需要使用cURL进行一些操作,因此需要通过服务器完成。

Here's a helpful PHP class that does the trick: Fusion Tables Client PHP

这是一个有用的PHP类,可以解决这个问题:Fusion Tables Client PHP

#1


6  

I was struggling with this a while back, and just this afternoon posted sample code and a working example of how to handle Fusion Tables queries.

我一直在努力争取这一点,就在今天下午发布了示例代码以及如何处理Fusion Tables查询的工作示例。

In a nutshell, Mark is exactly right about the same origin policy (http://en.wikipedia.org/wiki/Same_origin_policy), and was just about there with his solution aside from one detail - you need to specify "jsonp" datatype with $.get. Read on at the jQuery .get page.

简而言之,Mark完全相同的原始政策(http://en.wikipedia.org/wiki/Same_origin_policy),除了一个细节之外,他的解决方案就在那里 - 您需要指定“jsonp”数据类型用$ .get。请继续阅读jQuery .get页面。

Based on your original example, this should work:

根据您的原始示例,这应该工作:

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]);
}

#2


0  

Use JSONP and fusion like this:

像这样使用JSONP和fusion:

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);

Not tested, you'll got the idea? Don't know whether your lib supports any useful abstraction.

没有经过测试,你会明白吗?不知道你的lib是否支持任何有用的抽象。

#3


0  

You likely being prevented from accessing Google fusion tables due to the Same Origin Policy.

由于同源策略,您可能无法访问Google融合表。

Some ways to solve this is to proxy the request through your own server (running on the same domain as the page you are serving) or requesting the data in JSONP.

解决此问题的一些方法是通过您自己的服务器(在与您服务的页面相同的域上运行)或在JSONP中请求数据来代理请求。

If you append the parameter jsonCallback=<callback name here> to your Fusion Tables request, then you will get a JSONP response. For example, the request:

如果将参数jsonCallback = 附加到Fusion Tables请求,那么您将获得JSONP响应。例如,请求:

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

results in:

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"]]}})

This article from IBM should help get you understand JSONP and how to work with it: http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

IBM的这篇文章应该有助于您了解JSONP以及如何使用它:http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

#4


0  

When I thought about this I realised that the easiest way to run the request through your own server would be to literally pretend it was your own request i.e. in a file stored.

当我想到这一点时,我意识到通过你自己的服务器运行请求的最简单方法就是假装它是你自己的请求,即在存储的文件中。

So I just created a php script that included the contents on my own domain

所以我刚创建了一个包含我自己域名内容的php脚本

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

And that did the trick, either calling it from AJAX or directly. Here's what you're looking for:

这就是诀窍,无论是从AJAX还是直接调用它。这是你要找的东西:

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

#5


0  

While noiv11's answer does work, it doesn't cover authorization for those wondering as you can't manage the headers with JSONP. For this you will need to use real headers to retrieve the data so this will require a little work with cURL and therefore need to be done via a server.

虽然noiv11的答案确实有效,但它不包括那些想知道你无法用JSONP管理标题的人的授权。为此,您需要使用真实的标头来检索数据,因此需要使用cURL进行一些操作,因此需要通过服务器完成。

Here's a helpful PHP class that does the trick: Fusion Tables Client PHP

这是一个有用的PHP类,可以解决这个问题:Fusion Tables Client PHP