Letten
Letten

Reputation: 21

Google App script doPost doesn't work

I was trying code a BMI HTML web by Google app script. However, the result always didn't appear by doPost. My code is below :

Code.gs

function doPost(e){

 var h= e.parameter.h;
 var w = e.parameter.w;
 return 
HtmlService.createHtmlOutput("bmi="+w/((h*h)/10000));
}

function doGet(e)
{

var t = HtmlService.createTemplateFromFile('Index');
t.serviceUrl = ScriptApp.getService().getUrl();
return t.evaluate();

}

Index.html

<html>
<body>
<form id="bmiForm" action="<?= serviceUrl ?>" method="post">
 <div>height(cm): <input type="text" name="h"/></div>
 <div>weight(kg): <input type="text" name="w"/></div>
 <input type="submit" value="submit"/>
 </form>
 </body>
</html>

Look for Answer

Upvotes: 2

Views: 788

Answers (1)

Sourabh Choraria
Sourabh Choraria

Reputation: 2331

Identified the error this script threw in the browser console -

Refused to display 'https://script.google.com/a/exotel.in/macros/s/WhateverScriptID/exec' in a frame because it set 'X-Frame-Options' to 'sameorigin'.

Solution

Adding .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL) while returning doPost's HTML output.

Refer Class HtmlOutput > setXFrameOptionsMode(mode).

Code.gs

function doPost(e) {
  var h= e.parameter.h;
  var w = e.parameter.w;
  return HtmlService.createHtmlOutput("bmi="+w/((h*h)/10000)).setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}

function doGet(e) {
  var t = HtmlService.createTemplateFromFile('Index');
  t.serviceUrl = ScriptApp.getService().getUrl();
  return t.evaluate();
}

Index.html

This remains the same -

<html>

<body>
    <form id="bmiForm" action="<?= serviceUrl ?>" method="post">
        <div>height(cm):
            <input type="text" name="h" />
        </div>
        <div>weight(kg):
            <input type="text" name="w" />
        </div>
        <input type="submit" value="submit" />
    </form>
</body>

</html>

This will display the desired results.

Upvotes: 1

Related Questions