在Apps脚本中处理POST请求后重定向 [英] Redirect after processing a POST request in Apps Script

查看:131
本文介绍了在Apps脚本中处理POST请求后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在让Google脚本页面重定向回我的自定义URL时遇到问题。该脚本当前正在执行,但执行完毕后无法将其重定向回其上一页。

I have had an issue with getting the google scripts page to redirect back towards my custom URL. The script currently executes but I cant get it to redirect back to its previous page after it is finished executing.

此处是script.gs代码:

Heres the script.gs code:

function doPost(e) {

  try {
    Logger.log(e); // the Google Script version of console.log see: Class Logger
    record_data(e);

    // shorter name for form data
    var mailData = e.parameters;
    var name= String(mailData.name);
    var message= String(mailData.message);
    var email= String(mailData.email);
    var all= ("Name: "+name+"\nReply address: "+email+"\nMessage: "+message);


    // determine recepient of the email
    // if you have your email uncommented above, it uses that `TO_ADDRESS`
    // otherwise, it defaults to the email provided by the form's data attribute
    var sendEmailTo = (typeof TO_ADDRESS !== "undefined") ? TO_ADDRESS :     mailData.formGoogleSendEmail;

    MailApp.sendEmail({
        to: String(sendEmailTo),
        subject: String(mailData.subject),
        replyTo: String(mailData.email), // This is optional and reliant on your form actually collecting a field named `email`
        body: String(all)
    });    
    doGet();
    return HtmlService.createHtmlOutput('xxxxxxxxxx.com');
  } catch(error) { // if error return this
    Logger.log(error);
    return ContentService
        .createTextOutput(JSON.stringify({"result":"error", "error": error}))
        .setMimeType(ContentService.MimeType.JSON);
  }
}  

function doGet() {
  return HtmlService.createHtmlOutputFromFile('Index');
}

这是我的HTML代码:

Here is my HTML code:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="refresh" content="2;url=xxxxxxxxxx.com" />
    <base target="_top">
  </head>
  <body>
    Click <a href="xxxxxxxxxx.com">here</a> to go back.
  </body>
</html>

使脚本打开index.html页面的最佳方法是什么,这样我可以轻松地重定向回到自定义网址?

What would be the best way to make the script open the index.html page so I could easily redirect back to the custom URL?

推荐答案

这是处理POST请求后重定向的有效示例。

Here's a working example of redirecting after processing a POST request.

var REDIRECT_URL = "http://www.stackoverflow.com";

function doPost(e) {
  Logger.log("POST request");
  Logger.log(e)
  return redirect();
}

function doGet() {
  Logger.log("GET request");
  var template = HtmlService.createTemplateFromFile("form");
  template.url = ScriptApp.getService().getUrl();
  return template.evaluate();
}

function redirect() {
  return HtmlService.createHtmlOutput(
    "<script>window.top.location.href=\"" + REDIRECT_URL + "\";</script>"
  ); 
}



form.html



form.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
      <form action="<?= url ?>" method="post">
        <input type="text" name="test_field" value="test data">
        <button type="submit">Submit form</button>
      </form>
  </body>
</html>



用法



当我访问发布的Web应用程序(即使用GET请求),将显示简单的表单。

Usage

When I visit the published web app (i.e. using a GET request), I'm presented with the simple form.

提交该表格(即使用POST请求)后,我立即被重定向到 http://www.stackoverflow.com

Submitting that form (i.e. using a POST request), I'm immediately redirected to http://www.stackoverflow.com.

此输出在脚本日志中捕获:

This output is captured in the script log:

[18-06-19 10:39:04:731 PDT] POST request
[18-06-19 10:39:04:732 PDT] {parameter={test_field=test data}, contextPath=, contentLength=20, queryString=, parameters={test_field=[test data]}, postData=FileUpload}






关于代码示例,您具有:


Regarding your code sample, you have:

doGet();
return HtmlService.createHtmlOutput('xxxxxxxxxx.com');

这没有意义,因为您对<$ c $的结果不做任何事情c> doGet()。为了使 doGet()调用有用,将上面的内容替换为以下内容:

That doesn't make sense as you're not doing anything with the results of doGet(). In order to make the doGet() call useful, replace the above with the following:

return doGet();

这篇关于在Apps脚本中处理POST请求后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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