LoginSignup
0
0

More than 3 years have passed since last update.

GAS でAPIを作って公開する。

Posted at

この記事に目的。

表題の備忘

手順

1 コード

下記のようにJsonを返すWebアプリを作成


function doGet(e){
  var json;
  json = {
    "hoge" : "hoge",
    "foo" : "bar"
  }
  json = ContentService.createTextOutput(JSON.stringify(json));
  return json;
}

2 webアプリケーションとして公開

公開したらブラウザからアクセスしてみると、jsonが表示できるはず。
Chromeの拡張機能 json Viewer を使うと表示が楽ちん。

3 クライアント JS


var request = new XMLHttpRequest();

request.open('GET', "ここにGASで発行されたWebアプリのurl", true);
request.responseType = 'json';

request.onload = function () {
  var data = this.response;
  console.log(data);
  var o = document.createElement("span");
  o.innerHTML = data["foo"];
  document.body.appendChild(o);
};

request.send();

補足

外部ドメインからだと、クロスドメインの制約に引っかかると思う。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0