Trouble with Get request from Google Fusion tables

不想你离开。 提交于 2019-12-03 17:07:55

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.

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.

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

Use JSONP and fusion like this:

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.

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

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.

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:

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/

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 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:

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!