You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Html Form与Google App Script交互:提交后无法跳转至Thanks.html

Hey there! Let's dig into why your redirect to Thanks.html isn't working after your HTML form triggers doPost() in your Apps Script web app. I've run into this exact issue before, so here are the most common fixes and things to check:

1. Make sure doPost() returns the correct output

The #1 mistake people make is forgetting to explicitly return the Thanks.html page after processing the form data. Apps Script needs a valid HtmlOutput response to tell the browser what to load next. Here's the minimal working example:

function doPost(e) {
  // (Optional) Process your form data here, e.g., save to a spreadsheet
  // const sheet = SpreadsheetApp.getActiveSheet();
  // sheet.appendRow([e.parameter.yourFieldName]);

  // Critical line: Return the Thanks.html page
  return HtmlService.createHtmlOutputFromFile('Thanks');
}

If you skip the return statement, the browser gets an empty or invalid response, so it won't redirect anywhere.

2. Check if you're using AJAX without handling the redirect manually

If you added custom JavaScript to submit the form via AJAX (like fetch or XMLHttpRequest), you've probably used e.preventDefault() to stop the default form submission. That means the browser won't automatically jump to the response page—you have to handle it in your success callback:

// Frontend script in your HTML form
document.querySelector('form').addEventListener('submit', async (e) => {
  e.preventDefault(); // Stops default browser form submission
  const formData = new FormData(e.target);
  
  const response = await fetch('YOUR_WEB_APP_URL/exec', {
    method: 'POST',
    body: formData
  });

  if (response.ok) {
    // Option 1: Redirect to the Thanks page directly
    window.location.href = 'YOUR_WEB_APP_URL/exec?action=showThanks';
    // Option 2: Replace the current page with the Thanks HTML
    const thanksHtml = await response.text();
    document.body.innerHTML = thanksHtml;
  }
});

If you go this route, you might want to add a doGet() handler to serve the Thanks page when the action=showThanks parameter is present, just to keep things clean.

3. Verify your Web App deployment settings

Sometimes the issue isn't code-related—it's how you deployed the app:

  • Double-check the Who has access to the app setting. If it's set to "Only myself", external users (or even you in an incognito window) will get an error instead of the redirect.
  • Ensure Execute as is set correctly. If you're saving data to a spreadsheet, "Me" is usually the right choice, but if permissions are messed up, doPost() might fail silently.
4. Test with a minimal setup first

Since you mentioned stripping down features, go even simpler to isolate the problem:

  1. Create a bare-bones Thanks.html with just <h1>Thanks for submitting!</h1>
  2. Make doPost() only return this page (no data processing)
  3. Use a plain HTML form with method="POST" and action="YOUR_WEB_APP_URL/exec"
    If this works, gradually add back your data processing logic step by step to find where the redirect breaks.

内容的提问来源于stack exchange,提问作者Jurgen Cuschieri

火山引擎 最新活动