简体   繁体   English

无法在Google脚本创建的HTML中引用.gs文件

[英]unable to reference to .gs file within the google script created HTML

I am developing a web application using google script. 我正在使用Google脚本开发Web应用程序。 I created a simple login page within the google script. 我在google脚本中创建了一个简单的登录页面。 However, I tried to reference the .js(.gs) to the button to sent out the login info but it keep showing 400 error. 但是,我尝试将.js(.gs)引用到该按钮以发送登录信息,但它始终显示400错误。 I know I probably do it the wrong way. 我知道我可能做错了方式。 I just don't know how to do it. 我只是不知道该怎么做。

please help 请帮忙

  <script src="/login_click.gs"></script>///reference to the gs file
   .....
  <button type="submit" onclick="login()">Login</button>///the login button

Client-to-server comms in GAS GAS中的客户端到服务器通讯

That's because .gs files are executed on the server and not on the client side. 这是因为.gs文件是在服务器而不是客户端上执行的。 Apps script has its own implementation of asynchronous server calls via the google.script.run client API. Apps脚本通过google.script.run客户端API具有自己的异步服务器调用实现。

Using google.script.run is quite simple, you just call server-side functions by prepending their name like this google.script.run.yourServerFunction(yourParameters) and, if your server-side function has return statement, you can operate on the returned value via a callback client-side function passed to withSuccessHandler() method (if you want to pass anything client-side to callback function after server-side function finishes executing, you can do so via withUserObject(dataToPass) method call - just remember that the first argument of the success handler fucntion is always the value returned from server-side). 使用google.script.run非常简单,您只需在服务器端函数前面google.script.run.yourServerFunction(yourParameters)这样的名称即可调用它们,如果服务器端函数具有return语句,则可以在通过传递给withSuccessHandler()方法的回调客户端函数返回的值(如果要在服务器端函数完成执行后将任何客户端传递给回调函数,则可以通过withUserObject(dataToPass)方法调用来实现-只要记住成功处理程序功能的第一个参数始终是服务器端返回的值)。

Modified html 修改的html

In your case, a simple modification will do the trick (assuming you are calling a server-side login() function: 在您的情况下,简单的修改就可以解决问题(假设您正在调用服务器端login()函数:

<script type="text/javascript">
  function success(fromServer,fromClient) {
    //doSomething on success;
  }

  function failure(error) {
    //doSomething on failure;
  }

  function login() {
    var myObject = ''; //add something to callback on success;

    google.script.run
    .withSuccessHandler(success)
    .withFailureHandler(failure)
    .withUserObject(myObject)
    .login();
  }
</script>

Useful links 有用的链接

  1. google.script.run reference ; google.script.run 参考 ;
  2. Client-server communication guide ; 客户-服务器通讯指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 gs 文件中的变量作为 Google Apps 脚本中 HTML 文件的输入值? - How can I use the variable in the gs file as the input value in the HTML file in Google Apps script? 将 .gs(Google Apps 脚本)中的数据共享到<script> variable in html - Share data from .gs (Google Apps Script) to <script> variable in html Google 脚本从 code.gs 返回到 index.html - Google script return from code.gs to index.html 在 Google App Script 中将变量从 Code.gs 传递到 html - Passing variable from Code.gs to html in Google App Script 在 Google Apps 脚本中将变量从 .gs 共享到 .html - Share variable from .gs into .html in Google Apps Script 如何将元素值变量从 Javascript html 文件传递到 google apps 脚本中的 code.gs 文件? - How to pass an element-value variable from Javascript html file to code.gs file in google apps script? 如何在 Google Script App 中从 .gs 文件中获取 JSON - How to real JSON from .gs file in Google Script App .gs / .html-使用表单元素,HTML服务和电子表格的Google Apps脚本作为网络应用程序 - .gs/.html - Google Apps Script as a Web App using Form Elements, Html Service and a spreadsheet 在HTML文件中引用HTML文件? - Reference to HTML file within HTML file? 使用 html 对话框中输入字段的值使用 Google Apps 脚本在 code.gs 中进行 var - use value from input field in html dialog box to var in code.gs using Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM