用户名和密码未传递到同一页面

时间:2019-02-12 15:08:58

标签: javascript html google-apps-script web-applications google-sheets

该html登录页面应该采用有效的用户名和密码,并从Google表格中进行确认,然后提供对包含数据的页面的访问。如果输入了无效的用户名和密码,则应使用带有“无效的用户名或密码”消息的不同html页面刷新同一标签。 当我将目标元素定义为“ _blank”时,代码工作正常,但是当目标元素设置为“ _self”时,显示“ script.google.com无法连接”错误

login.html

     <script>
        google.script.run.withFailureHandler(failed).
    withSuccessHandler(dropdown).getDropdown("Offices", "A", 3, "list");

    function dropdown(dropdown){
       var datalist = document.getElementsByClassName(dropdown[0]);
           var len = dropdown.length;
           var options = ""; 
           for (i = 1; i < len; i++) {
               options = options + "<option value='" + dropdown[i] + "'>" + 
    dropdown[i] + "</option>";
             }
             for (n in datalist) {
                 datalist[n].innerHTML = options;
             } 
     }


function failed(e){
alert("Error:", +e);
}

</script>
      <form 
         action= "https://script.google.com/a/macros/caplineservices.com/s/
         AKfycbw1BhOakZMzoGnErWw2uBuZzDawn8ThQjEQpBm_nSd6Eyb3NITF/exec"
         method="POST" target="_self" 
         align="right" id="loginForm" style="transform:translateX(-5%)" >

    <h4 style="font-size:40px;
 font-family: 'Trebuchet MS', Arial, Helvetica, sans-serif; 
color:#4fbcef">Login</h4>

   <p> Select Office: <select class="list" name="officeName" 
style="padding: 3px;width:200px;color:#ffffff;" ></select></p>

   <p style=""> Username: <input type="text" name="userName" 
placeholder="Username" style="padding: 3px;width:200px;">
    </p>
    <p> Password: <input type="password" name="password" 
placeholder="Password" style="padding:3px;width:200px;"> 
        </p>
    <input type="submit" value="Login" align="center" class="submit">
    </form>
  </body>
</html>

post.gs文件

function doPost(e){
if(e.parameter.officeName!="" && e.parameter.userName=="admin" && 
e.parameter.password=="admin"){
 return ivDashboard(e.parameter.officeName);
  }
else{
return faulty();
  }
}

function ivDashboard(officeName){
   var html = HtmlService.createTemplateFromFile("index");
   html.officeName = officeName;
   return html.evaluate();
}

function faulty(){
var html=HtmlService.createTemplateFromFile("faultyLogin");
return html.evaluate();
}

faultyLogin.html

     <script>
         google.script.run.withFailureHandler(failed).
    withSuccessHandler(dropdown).getDropdown("Offices", "A", 3, "list");

       function dropdown(dropdown){
          var datalist = document.getElementsByClassName(dropdown[0]);
                var len = dropdown.length;
                var options = ""; 
                for (i = 1; i < len; i++) {
                    options = options + "<option value='" + dropdown[i] + 
    "'>" + dropdown[i] + "</option>";
                }
                for (n in datalist) {
                    datalist[n].innerHTML = options;
                 } 
        }


function failed(e){
alert("Error:", +e);
}

</script>

     <form  
 action="https://script.google.com/a/macros/caplineservices.com/s/
AKfycbw1BhOakZMzoGnErWw2uBuZzDawn8ThQjEQpBm_nSd6Eyb3NITF/exec" 
    method="POST" target="_blank" align="right" id="loginForm" 
style="transform:translateX(-100px)">
    <!-- IV Form web app url where the database sheet is linked and request 
will be processed-->
    <h4 style="font-size:40px;
 font-family: 'Trebuchet MS', Arial, Helvetica, serif; 
color:#4fbcef">Login</h4>
 <p style="color:red;font-size:110%;font-style:italic;"> Please enter 
correct username or password.</p>    
   <p> Select Office: <select class="list" name="officeName" 
style="padding: 3px;width:200px;color:#ffffff;" ></select></p>

   <p style=""> Username: <input type="text" name="userName" 
placeholder="Username" style="padding: 3px;width:200px;">
    </p>
    <p> Password: <input type="password" name="password" 
placeholder="Password" style="padding:3px;width:200px;"> 

    </p>
    <input type="submit" value="Login" align="center" class="submit">
    </form>

1 个答案:

答案 0 :(得分:0)

由于脚本在iframe中运行,因此target只能是_blank_top 1。使用_top加载第一帧。