Google App脚本doPost不起作用

时间:2018-08-24 08:27:17

标签: google-apps-script

我正在尝试通过Google应用程序脚本编写BMI HTML网站。但是,结果始终不会由doPost出现。 我的代码如下:

Code.gs

<html>
<body>
<form id="bmiForm" action="<?= serviceUrl ?>" method="post">
 <div>height(cm): <input type="text" name="h"/></div>
 <div>weight(kg): <input type="text" name="w"/></div>
 <input type="submit" value="submit"/>
 </form>
 </body>
</html>

Index.html

inline-flex

寻找答案

1 个答案:

答案 0 :(得分:4)

确定了此脚本在浏览器控制台中引发的错误-

  

拒绝在帧中显示“ https://script.google.com/a/exotel.in/macros/s/WhateverScriptID/exec”,因为它将“ X-Frame-Options”设置为“ sameorigin”。

解决方案

在返回.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)的HTML输出的同时添加doPost

引用 Class HtmlOutput > setXFrameOptionsMode(mode)

Code.gs

function doPost(e) {
  var h= e.parameter.h;
  var w = e.parameter.w;
  return HtmlService.createHtmlOutput("bmi="+w/((h*h)/10000)).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

function doGet(e) {
  var t = HtmlService.createTemplateFromFile('Index');
  t.serviceUrl = ScriptApp.getService().getUrl();
  return t.evaluate();
}

Index.html

这保持不变-

<html>

<body>
    <form id="bmiForm" action="<?= serviceUrl ?>" method="post">
        <div>height(cm):
            <input type="text" name="h" />
        </div>
        <div>weight(kg):
            <input type="text" name="w" />
        </div>
        <input type="submit" value="submit" />
    </form>
</body>

</html>

这将显示所需的结果。