我如何可以加载从一个客户端应用程序融合表的CSV? [英] How can I load a Fusion Tables csv from a client application?
问题描述
我只是在玩谷歌融合表,我想知道我怎么能加载从客户端的CSV。到目前为止,我已经尝试了几种选择:
从ActionScript 3.0:
变种R:的URLRequest =新的URLRequest(https://www.google.com/fusiontables/exporttable?query=select%20*%20from%203685185%20);
r.method = URLRequestMethod.GET;
VAR L:的URLLoader =新的URLLoader(R);
l.addEventListener(引发Event.COMPLETE,加载);
l.addEventListener(HTTPStatusEvent.HTTP_STATUS,中的onHTTPStatus);
函数中的onHTTPStatus(事件:HTTPStatusEvent):无效{
跟踪(event.status);
}
功能加载(事件:事件):无效{
跟踪(this.loaderInfo.url,event.target.data);
}
从ActionScript 2.0:
VAR瓦尔:Load忱=新的LoadVars();
vars.onLoad =功能(加载):无效{
如果(加载)跟踪(反向转义(本));
其他痕迹(错误加载数据);
}
vars.onHTTPStatus =功能(状态:编号):无效{
跟踪(状态);
}
vars.load("http://www.google.com/fusiontables/exporttable?query=select%20*%20from%203685185%20&r="+new 。日期()getMilliseconds());
从JavaScript:
<$p$p><$c$c>$.get('https://www.google.com/fusiontables/exporttable?query=select%20*%20from%203685185%20', 功能(数据){警报(数据); });在ActionScript中一切正常,在独立播放器,但不能上网,气味似某种安全沙箱问题。 在 JS 我得到这样的:
XMLHtt prequest无法加载https://www.google.com/fusiontables/exporttable?query=select%20*%20from%203685185%20。原产地http://lifesine.eu不受访问控制 - 允许 - 原产地允许的。
我查询的表是公开的,可导出。我也试着使用简单的REST客户端上的Chrome浏览器的呼叫,并得到了正确的反应。关于怎么样我可能会丢失任何提示?
我只能说给javascript的方法。您需要使用JSONP来检索由于浏览器跨域访问限制的结果。幸运的是融合表支持JSONP。我发布了一些例子code在此回答。另一种方法是使用google.visualization库,并有一些示例code在此回答。
更新 @乔治 - profenza
2分。的URL FT JSONP API是不同的。而对于JSONP必须添加一个回调参数。试试这个:
VAR queryurl = 'https://fusiontables.googleusercontent.com/fusiontables/api/query?sql=select%20*%20from%203685185%20&jsonCallback=?';
另外这里有一个例子成功的功能:
函数的DataHandler(数据,textStatus,jqXHR){
警报(textStatus);
VAR COLS = data.table.cols;
VAR行= data.table.rows;
对于(VAR I = 0; I&LT; cols.length;我++){
警报(COLS [I]);
}
对于(VAR I = 0; I&LT; rows.length;我++){
为(J = 0; J&LT;行[I] .length; J ++){
警报(行[I] [J]);
}
}
}
I was just playing with Google Fusion Tables and I was wondering how I could load a csv from the client side. So far I've tried several options:
From actionscript 3.0:
var r:URLRequest = new URLRequest("https://www.google.com/fusiontables/exporttable?query=select%20*%20from%203685185%20");
r.method = URLRequestMethod.GET;
var l:URLLoader = new URLLoader(r);
l.addEventListener(Event.COMPLETE,loaded);
l.addEventListener(HTTPStatusEvent.HTTP_STATUS,onHTTPStatus);
function onHTTPStatus(event:HTTPStatusEvent):void{
trace(event.status);
}
function loaded(event:Event):void{
trace(this.loaderInfo.url,event.target.data);
}
From actionscript 2.0:
var vars:LoadVars = new LoadVars();
vars.onLoad = function(loaded):Void{
if(loaded) trace(unescape(this));
else trace("error loading data");
}
vars.onHTTPStatus = function(status:Number):Void{
trace(status);
}
vars.load("http://www.google.com/fusiontables/exporttable?query=select%20*%20from%203685185%20&r="+new Date().getMilliseconds());
From javascript:
$.get('https://www.google.com/fusiontables/exporttable?query=select%20*%20from%203685185%20',
function(data) { alert(data); });
In actionscript everything works in the standalone player, but not online which smells like some sort of security sandbox issue. In JS I get this:
XMLHttpRequest cannot load https://www.google.com/fusiontables/exporttable?query=select%20*%20from%203685185%20. Origin http://lifesine.eu is not allowed by Access-Control-Allow-Origin.
The table I'm querying is public and exportable. I've also tried the call using Simple REST Client on Chrome and got the right response. Any hints on how what I might be missing ?
I can only speak to the javascript approach. You need to use JSONP to retrieve the results due to browser cross domain access restrictions. Luckily Fusion Tables supports JSONP. I posted some example code in this answer. Another approach would be to use the google.visualization library and there's some sample code in this answer.
UPDATE @george-profenza
2 points. The URL for the FT JSONP api is different. And for jsonp you must add a callback parameter. Try this:
var queryurl = 'https://fusiontables.googleusercontent.com/fusiontables/api/query?sql=select%20*%20from%203685185%20&jsonCallback=?';
Also here's an example success function:
function dataHandler(data, textStatus, jqXHR) {
alert(textStatus);
var cols = data.table.cols;
var rows = data.table.rows;
for (var i = 0; i < cols.length; i++) {
alert(cols[i]);
}
for (var i = 0; i < rows.length; i++) {
for(j=0; j < rows[i].length; j++) {
alert(rows[i][j]);
}
}
}
这篇关于我如何可以加载从一个客户端应用程序融合表的CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!