[英]Pass value from ajax to Google Apps Script
I'm new to Google Apps Script and I'm trying to make a web app where I can pass the value from Ajax to my script's web app.我是 Google Apps Script 的新手,我正在尝试制作一个网络应用程序,我可以在其中将值从 Ajax 传递到我脚本的网络应用程序。 I'm able to execute it directly from the web app but it's not working from my webpage.
我可以直接从网络应用程序执行它,但它不能从我的网页上运行。 Here are my codes,
这是我的代码,
Webpage's JS网页的JS
$(document).ready(function () {
$('#btnGet').click(function () {
var getId = $('#text').val();
$.ajax({
url: "https://script.google.com/macros/s/AKfycbwpkBW7qKZBeJ011C7j3le4vV8D0SEHu9709mWtEMzJgrJmHnaR/exec",
data: getId,
type: "GET",
crossDomain: true,
success: function (data) {
console.log(data);
alert("Success!");
},
error: function (data) {
console.log(data);
alert("Failed!");
}
});
});
});
Google Apps Script Google Apps 脚本
function doGet(e) {
var html ="<input type='text' id='text' /><br><br><input type='button' id='btnGet' onclick='myClick()' value='submit' /><br><br>";
html+="<script>";
html+="document.getElementById('btnGet').onclick = function() { var e = document.getElementById('text').value; google.script.run.newEntry(e); }</script>";
return HtmlService.createHtmlOutput(html);
}
function newEntry(text) {
Logger.log(text);
var ss = SpreadsheetApp.openById(text);
var sheet = ss.getSheetByName("Sheet1");
var lr = sheet.getLastRow();
sheet.getRange(lr+1, 1, 1, 3).setValues([["First Name", "Last Name", "Email"]]);
}
How can I pass the value from ajax to my apps script?如何将值从 ajax 传递到我的应用程序脚本?
Try appending the data you're sending out onto the end of the url:尝试将您发送的数据附加到 url 的末尾:
"ScriptUrl/exec?getIdKey=getIdValue"
Your data
property is likely just being ignored.您的
data
属性可能只是被忽略了。
The value can then be accessed in doGet(e)
with:然后可以在
doGet(e)
访问该值:
var getIdValue = e.parameter.getIdKey
You will have to make a page redirection and navigate to the script's published url on a click or something您必须进行页面重定向并通过单击或其他方式导航到脚本的已发布网址
because XHR requests to the Google Apps Script server are forbidden
因为禁止向 Google Apps 脚本服务器发送 XHR 请求
From an external client XHR requests are forbidden but within the Google App Script editor calls to server side scripts(ie any one of the functions in .gs file) are by default asynchronous and can be made using google.script.run
来自外部客户端的 XHR 请求被禁止,但在 Google App Script 编辑器中对服务器端脚本(即 .gs 文件中的任何一个函数)的调用默认是异步的,可以使用
google.script.run
For more please refer to the official documentation.更多请参考官方文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.