3

我正在尝试设置一个页面,允许访问者输入信息,然后将这些信息直接发布到 Google Fusion 表(而不是 mysql 数据库)。

我听说 Google 正在摆脱旧的 Fusion Tables SQL API,所以我需要符合 Fusion Tables API v.1 的东西。我知道它应该以 POST 请求的形式出现,格式如下: INSERT INTO <tableid> (column name) VALUES (user-input response),但在尝试之后多次迭代,我没有任何成功。

最后,我求助于将基本表单输入的示例代码复制到融合表,将值更改为我自己的示例表。这仍然行不通。这是我的代码 - 我希望在这方面有一些经验的人能够评估。我真的不知道我在做什么,我猜。

<!DOCTYPE html>
<html>
  <head>

<style type="text/css">
  body { font-family: Arial; } 
</style>
<script src="https://apis.google.com/js/client.js?onload=initialize"></script>
<script type="text/javascript">
  // The clientId and apiKey are available at
  // https://code.google.com/apis/console. For more information, see
  // http://code.google.com/p/google-api-javascript-client/wiki/Authentication.

  var clientId = '424838770953.apps.googleusercontent.com';
  var apiKey = 'AIzaSyBGFOBBGu5x2LuCNmDIvfe-FEPtq--tRPI';

  var scopes = 'https://www.googleapis.com/auth/fusiontables';
  var tableId = '1c9tCQTEOog66RXBCQxNjmHDERbIJxuAm5SgBhSs';

  // Initialize the client, set onclick listeners.
  function initialize() {
    gapi.client.setApiKey(apiKey);
    document.getElementById('insert-data').onclick = insertData;
    window.setTimeout(function() { auth(true); }, 1);
  }

  // Run OAuth 2.0 authorization.
  function auth(immediate) {
    gapi.auth.authorize({
      client_id: clientId,
      scope: scopes,
      immediate: immediate
    }, handleAuthResult);
  }

  // Handle the results of the OAuth 2.0 flow.
  function handleAuthResult(authResult) {
    var authorizeButton = document.getElementById('authorize-button');
    var createTableButton = document.getElementById('create-table');
    if (authResult) {
      authorizeButton.disabled = true;
      createTableButton.disabled = false;
    } else {
      authorizeButton.disabled = true;
      authorizeButton.onclick = function() { auth(false); return false; };
    }
  }



  // Run a request to INSERT data.
 runClientRequest({
      path: '/fusiontables/v1/tables',

      method: 'POST'
    },  function insertData() {
    var name = document.getElementById('name').value;
    var age = document.getElementById('age').value;
    var insert = [];
    insert.push('INSERT INTO ');
    insert.push('1c9tCQTEOog66RXBCQxNjmHDERbIJxuAm5SgBhSs');
    insert.push(' (Name, Age) VALUES (');
    insert.push("'" + name + "', ");
    insert.push(age);
    insert.push(')');

    body: 'insert'
  }

  // Execute the client request.
  function runClientRequest(request, callback) {
    var restRequest = gapi.client.request(request);
    restRequest.execute(callback);
  }
</script>
</head>
<body>
<h1>
Fusion Tables JavaScript Example
</h1>
<h2>
(3) Insert data
</h2>
<p>
  Insert data into the newly created table.
</p>
<pre>INSERT INTO <span id="table-id-1">[table_id]</span> (Name, Age) VALUES ([name], 
[age])</pre>
<label>Name:</label>
<input type="text" id="name"><br>
<label>Age:</label>
<input type="age" id="age"><br>
<input type="button" id="insert-data" value="Insert data">
<p id="insert-data-output"><i>insert response goes here...</i></p><br>

4

0 回答 0