doPost not working in Google app script

后端 未结 4 2211
慢半拍i
慢半拍i 2020-12-29 14:09

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)         


        
4条回答
  •  半阙折子戏
    2020-12-29 14:52

    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


    UPDATE 2018-07-18

    Apps Script now supports StackDriver Logging which is accessible from the Apps Scripts editor's View menu.

提交回复
热议问题