调用doPost()的Google AppScripts从服务器触发

时间:2018-12-29 10:21:58

标签: html post google-apps-script web-applications

当我尝试从服务器调用doPost()时,我遇到了意外的响应。

查看以下脚本:

function doPost(e) {
  var params = e.parameter;

  return HtmlService.createHtmlOutput(JSON.stringify(params));
}

当我向该脚本发送POST请求时,期望得到{}作为响应。当我使用邮递员发送POST请求时,收到以下响应:

<!doctype html><html><head><meta name="chromevox" content-script="no"><link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"><link rel="stylesheet" href="/static/macros/client/css/189396996-mae_html_css_ltr.css"><script type="text/javascript" src="/static/macros/client/js/3229800183-warden_bin_i18n_warden.js"></script></head><body role="main"><table id="warning-bar-table" class="full_size" cellspacing="0" cellpadding="0"><tr><td><div id="warning" class="warning-bar"></div></td></tr><tr><td style="height: 100%"><iframe id="sandboxFrame" allow="accelerometer *; ambient-light-sensor *; autoplay *; camera *; encrypted-media *; fullscreen *; geolocation *; gyroscope *; magnetometer *; microphone *; midi *; payment *; picture-in-picture *; speaker *; usb *; vibrate *; vr *" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation"></iframe></td></tr></table><script type="text/javascript">
(function() {
var el = document.getElementById('sandboxFrame');
el.onload = function() {
goog.script.init("...");}
el.src = 'https:\/\/n--0lu-script.googleusercontent.com\/userCodeAppPanel';
}());
</script></body></html>

查看此结果,看起来该脚本仅应从浏览器运行。但是拥有doGet()doPost()使我怀疑Google也希望这样的调用也能正常工作。有人可以澄清发生了什么事吗?

1 个答案:

答案 0 :(得分:2)

POST确实有效。您将使用HtmlService返回html,默认情况下,它将把Google的加载代码注入到所提供的html中。请改用ContentService

return ContentService.createTextOutput(JSON.stringify(params));