为什么我无法在我的 Google 融合 table 中获取行数?
Why can I not get number of rows in my Google fusion table?
我(javascript 等的新手)制作了以下网页来获取我的 Google 融合 table 中的行数。我的 div 收到结果却显示“0”表示错误。有什么建议么? (我也不确定我必须在
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>test count</title>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
<script type="text/javascript">
function dataHandler(resp)
{
try
{$('#result').html((resp.table.rows[1][1]));}
catch(err)
{$('#result').html('0');}
};
function getData()
{
var tableId = '1G2iUAdGUAcEGCTfDykKnURZvY1QFe6pxoBKqi8A4';
var queryUrlHead = 'http://www.google.com/fusiontables/api/query?sql=';
var queryUrlTail = '&jsonCallback=getData';
var query = "SELECT COUNT() FROM " + tableId ;
window.alert(queryUrlHead + query + queryUrlTail);
var queryurl = encodeURI(queryUrlHead + query + queryUrlTail);
var jqxhr = $.get(queryurl, dataHandler, "jsonp");
};
function initialize()
{
getData();
dataHandler();
};
</script>
</head>
<body onload="initialize()">
<div id="result">xx</div>
</body>
</html>
API的URL不正确,应该是
https://www.googleapis.com/fusiontables/v2/query
- FusionTable-Queries 至少需要一个有效的键
- 可通过
resp.rows[0][0]
访问所需信息
- 去掉initialize()中对
dataHandler();
的调用
固定脚本(插入您自己的有效密钥):
function dataHandler(resp)
{
try
{$('#result').html((resp.rows[0][0]));}
catch(err)
{$('#result').html('0');}
};
function getData()
{ var yourApiKey='insert your key here';
var tableId = '1G2iUAdGUAcEGCTfDykKnURZvY1QFe6pxoBKqi8A4';
var queryUrlHead = 'https://www.googleapis.com/fusiontables/v2/query?sql=';
var queryUrlTail = '&jsonCallback=getData&key='+yourApiKey;
var query = encodeURIComponent("SELECT COUNT() FROM " + tableId) ;
var queryurl = queryUrlHead + query + queryUrlTail;
var jqxhr = $.get(queryurl, dataHandler, "jsonp");
};
function initialize()
{
getData();
};
我(javascript 等的新手)制作了以下网页来获取我的 Google 融合 table 中的行数。我的 div 收到结果却显示“0”表示错误。有什么建议么? (我也不确定我必须在
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>test count</title>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script>
<script type="text/javascript">
function dataHandler(resp)
{
try
{$('#result').html((resp.table.rows[1][1]));}
catch(err)
{$('#result').html('0');}
};
function getData()
{
var tableId = '1G2iUAdGUAcEGCTfDykKnURZvY1QFe6pxoBKqi8A4';
var queryUrlHead = 'http://www.google.com/fusiontables/api/query?sql=';
var queryUrlTail = '&jsonCallback=getData';
var query = "SELECT COUNT() FROM " + tableId ;
window.alert(queryUrlHead + query + queryUrlTail);
var queryurl = encodeURI(queryUrlHead + query + queryUrlTail);
var jqxhr = $.get(queryurl, dataHandler, "jsonp");
};
function initialize()
{
getData();
dataHandler();
};
</script>
</head>
<body onload="initialize()">
<div id="result">xx</div>
</body>
</html>
API的URL不正确,应该是
https://www.googleapis.com/fusiontables/v2/query
- FusionTable-Queries 至少需要一个有效的键
- 可通过
resp.rows[0][0]
访问所需信息
- 去掉initialize()中对
dataHandler();
的调用
固定脚本(插入您自己的有效密钥):
function dataHandler(resp)
{
try
{$('#result').html((resp.rows[0][0]));}
catch(err)
{$('#result').html('0');}
};
function getData()
{ var yourApiKey='insert your key here';
var tableId = '1G2iUAdGUAcEGCTfDykKnURZvY1QFe6pxoBKqi8A4';
var queryUrlHead = 'https://www.googleapis.com/fusiontables/v2/query?sql=';
var queryUrlTail = '&jsonCallback=getData&key='+yourApiKey;
var query = encodeURIComponent("SELECT COUNT() FROM " + tableId) ;
var queryurl = queryUrlHead + query + queryUrlTail;
var jqxhr = $.get(queryurl, dataHandler, "jsonp");
};
function initialize()
{
getData();
};