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.
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);})}
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:
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
Use JSONP and fusion like this:
Not tested, you'll got the idea? Don't know whether your lib supports any useful abstraction.
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
And that did the trick, either calling it from AJAX or directly. Here's what you're looking for:
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:results in:
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/