![](/img/trans.png)
[英]Getting Developer Metadata using Google Script API on Google Apps Script
[英]Getting CORS using JS and Google Apps script
我正在使用 Google Apps 腳本,我只是嘗試從前端的 javascript 連接到 Google 腳本,但我不斷收到 CORS 錯誤。
CORS 策略已阻止從源“null”訪問“https://script.google.com/macros/s/KEY/exec”處的 XMLHttpRequest請求的資源。
在 JS 我有這個
var email = $('#ID_EMAIL').val();
$.post("https://script.google.com/macros/s/KEY/exec",
{
email: email
},
function (data) {
console.log("success");
}, 'json');
並在 GAS 中
function doPost(e) {
return "hello";
}
如果只是基本代碼,我不知道為什么我會不斷收到錯誤消息。
我已將 google 表格發布為 web 頁面,並將 GAS 作為 web 應用程序向任何人公開。
我不知道我錯過了什么
這件事不久前發生在我身上,讓我發瘋。 希望我能減輕你的痛苦。 最終的問題(在我看來)是錯誤消息實際上沒有幫助,因為它會將您發送到一個實際上沒有用的 CORS 兔子洞。
要使 doPost / doGet 方法按預期工作,您必須返回ContentService object。 因此,如果您將 doPost 更改為以下內容,您應該會很好:
function doPost(e) {
return ContentService.createTextOutput("hello");
}
或者,如果您想返回 json:
return ContentService
.createTextOutput(JSON.stringify({
"result": "success",
"success": "hello"
}))
.setMimeType(ContentService.MimeType.JSON);
確保在進行更改后重新部署您的 webapp。
對您來說似乎並非如此,但是值得一提的是,如果 google 腳本 web 應用程序中有任何致命錯誤,您將看到 CORS 錯誤(至少在我的經驗中)。 例如,曾幾何時,我看到了這個錯誤,因為雖然我返回了一個 ContentService object,但我調用了一個尚未定義的變量,所以腳本在返回語句之前出錯了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.