doPost不适用于Google应用程序脚本 [英] doPost not working in Google app script

查看:117
本文介绍了doPost不适用于Google应用程序脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过各种问题,但他们都不能解决我的问题。我在谷歌应用程序脚本中写了一个简单的 doPost()代码:

 函数doPost(e){
Logger.log(Hello World);
}

然后,我将它部署为一个Web应用程序并将该URL粘贴到hurl.it发布帖子请求。但是,日志中没有记录,响应是200(好)。我认为它不在这个 doPost()函数内部。任何人都可以指导我在这里做错了什么?

解决方案

您的实现不符合Web应用程序所需的所有要求。以下是文档摘录(链接):


网络应用的要求

脚本可以发布为一个Web应用程序,如果它满足这些要求:


  • 它包含一个 doGet(e) doPost(e)函数

  • 该函数返回一个HTML服务 HtmlOutput 对象或内容服务 TextOutput 对象


以下是一些示例:

 函数doGet(e){
var params = JSON.stringify E);
返回HtmlService.createHtmlOutput(params);
}

函数doPost(e){
return ContentService.createTextOutput(JSON.stringify(e.parameter));



$ b

为了保持完整性,您还必须将您的Web App重新部署为新版本每次你修改代码。在现有版本下重新部署不起作用,您必须为您的更改制作一个新版本。



同样使用标准的 Logger .log 来跟踪 doGet(e) doPost(e)内的更改是不可靠的使用网络应用程序,因为它们是异步执行的。我建议将您的输出记录到电子表格。有一个称为BetterLog的令人敬畏的脚本库,它扩展了Logger API来做到这一点;它可以在以下链接找到:



https: //github.com/peterherrmann/BetterLog


I came across various questions but none of them could solve my problem. I wrote a simple doPost() code in google app script:

function doPost(e){
  Logger.log("Hello World");
}

Then I deployed it as a web app and pasted the url on hurl.it to make a post request. However, there is nothing being logged in the log and the response is 200 (Ok). I think it is not going inside this doPost() function. Can anyone guide as to what am I doing wrong here?

解决方案

Your implementation does not meet all the requirements needed for a web app. Here's an excerpt from the documentation (link):

Requirements for web apps

A script can be published as a web app if it meets these requirements:

  • It contains a doGet(e) or doPost(e) function.
  • The function returns an HTML service HtmlOutput object or a content service TextOutput object.

Here are some examples:

function doGet(e) {
  var params = JSON.stringify(e);
  return HtmlService.createHtmlOutput(params);
}

function doPost(e) {
  return ContentService.createTextOutput(JSON.stringify(e.parameter));
}

And just for completeness, you must also redeploy your web App as a new version every time you make changes to the code. Redeploying under an already existing version does not work, you have to make a new version for your changes to take hold.

Also using the standard Logger.log to trace changes within doGet(e) or doPost(e) is unreliable with web apps as they are executed asynchronously. I would recommend logging your output to a spreadsheet. There is an awesome script library called BetterLog that extends the Logger API to do just that; it can be found at the following link:

https://github.com/peterherrmann/BetterLog

这篇关于doPost不适用于Google应用程序脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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