使用HtmlService在Google Apps脚本中创建POST表单? [英] Do Form POST in Google Apps Script with HtmlService?

查看:110
本文介绍了使用HtmlService在Google Apps脚本中创建POST表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google Apps脚本制作向导类型的应用程序。由于UiApp的样式在后面非常痛苦,我正在尝试使用HtmlService路由。我无法获得表单POST的工作。这里是我的示例代码:

  // Code.gs 
函数doGet(e){
return HtmlService .createHtmlOutputFromFile( 'P1');
}

函数doPost(e){
var params = Utilities.jsonStringify(e);
var page = HtmlService.createTemplateFromFile('p2');
page.params = params;
返回page.evaluate()。setSandboxMode(HtmlService.SandboxMode.NATIVE);
}

p1.html

 < DIV> 
< form action =#method =post>
< input type =textid =myContents>< / input>
< input type =submitvalue =Submit>< / input>
< / form>
< / div>

p2.html

 < DIV> 
<?= params?>
< / div>

通过点击p1上的Submit得到的输出是


{queryString:null,parameter:{},contextPath:,parameters:{},contentLength:0}


我期待表单将数据返回给我,以便我可以在后续页面中使用数据。我想知道这是否与Caja中毒有关。任何想法?

解决方案

由于 Google Caja。



在HtmlService中做表单发布的正确方法是使用 google.script.run 语法在客户端上下文中调用服务器端函数。请参阅此处以获取详细示例 - https://developers.google。 com / apps-script / guides / html-service-communication#表单

I'm trying to do a wizard type application with Google Apps Script. Since styling with UiApp is so pain in the rear, I'm trying with the HtmlService route. I can't get the form POST working. Here's my sample code:

// Code.gs
function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('p1');
}

function doPost(e) {
  var params = Utilities.jsonStringify(e);
  var page = HtmlService.createTemplateFromFile('p2');
  page.params = params;
  return page.evaluate().setSandboxMode(HtmlService.SandboxMode.NATIVE);
}

p1.html

<div>
  <form action="#" method="post">
    <input type="text" id="myContents"></input>
    <input type="submit" value="Submit"></input>
  </form>
</div>

p2.html

<div>
  <?=params?>
</div>

The output I've gotten from clicking "Submit" on p1 is

{"queryString":null,"parameter":{},"contextPath":"","parameters":{},"contentLength":0}

I'm expecting the form to return data to me so that I can use the data in the consequent page. I'm wondering if this had anything to do with Caja poisoning. Any ideas?

解决方案

Posting itself doesn't work in HtmlService due to security restrictions imposed by Google Caja.

The correct way to do "form posts" in HtmlService is using google.script.run syntax to call server side functions with client side context. See here for a detailed example - https://developers.google.com/apps-script/guides/html-service-communication#forms

这篇关于使用HtmlService在Google Apps脚本中创建POST表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆