我在 Apps Script 中為 google sheet 定義了這個腳本:
function doPost(e) {
var app = SpreadsheetApp.getActive();
var sheet = app.getSheetByName('Web');
var content = JSON.parse(e.postData.contents)
sheet.getRange("A1").setValue(content);
return ContentService.createTextOutput("Saved");
}
我將其實作為Web Application并將訪問權限設定為Anyone.
然后嘗試通過郵遞員將發布請求發送到生成的 url,它完美地作業。還嘗試了其他郵遞員之類的應用程式。一切都沒有問題。請求的內容正文始終存盤在“A1”單元格中。

Fetch API但是當我想使用我的Vue.js網路應用程式發送完全相同的請求時。它失敗并出現錯誤:TypeError: Failed to fetch。無法從錯誤中讀取任何其他內容。沒有狀態碼,什么都沒有。從我的網路應用程式單擊按鈕時呼叫的函式的主體:
function sendForm() {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
var raw = JSON.stringify({
"test": "testing from vue"
});
var requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow'
};
fetch("https://script.google.com/macros/s/<id of the implementation>/exec", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
}
javascript代碼是從郵遞員本身生成的,所以應該沒有區別。
這是控制臺輸出:

也試過了axios。類似的問題,但錯誤不同:Network Error.

我不知道該做什么或去哪里看。我用谷歌搜索了幾個小時,但沒有發現任何幫助。如果有人知道可能是什么原因,我將非常感激。
uj5u.com熱心網友回復:
從Then tried sending post request through postman to the generated url and it worked flawlesly.,我確認您的 Web 應用程式可以正確使用。在這種情況下,在你的腳本的情況下,下面的修改怎么樣?
修改后的腳本:
function sendForm() {
var raw = JSON.stringify({"test": "testing from vue"});
var requestOptions = {
method: 'POST',
body: raw,
};
fetch("https://script.google.com/macros/s/<id of the implementation>/exec", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
}
- 當我使用您的 Web Apps 腳本測驗此修改后的腳本時,我確認已將
{test=testing from vue}其放入單元格中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/442351.html
標籤:javascript Vue.js 谷歌应用脚本 谷歌表格 邮政
上一篇:使用ANTLR生成的源檔案
下一篇:WebApp沒有通過UrlFetchApp.fetch()激活,但是當我在瀏覽器地址欄中鍵入URL路徑時,它運行完美
