为什么我的Google融合表中没有行数?

时间:2015-03-30 10:42:02

标签: javascript json google-fusion-tables

我(javascript等的新手)制作了以下网页以获取我的Google融合表格中的行数。我接受结果的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>

1 个答案:

答案 0 :(得分:1)

  1. API的网址不正确,应为

    https://www.googleapis.com/fusiontables/v2/query
    
  2. FusionTable-Queries至少需要一个有效密钥
  3. 可通过resp.rows[0][0]
  4. 访问所需信息
  5. 在initialize()
  6. 中删除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();
    
    };