调用Google Apps Script POST请求报错:CORS及doGet未找到问题排查
Google Apps Script POST请求相关问题
报错情况
- Expo Web应用控制台报错:
Access to fetch at 'https://script.google.com/macros/s/MyScriptID/exec' from origin 'http://localhost:8081' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
- 直接访问该Google Apps Script链接时,页面提示:
Script function not found: doGet
个人判断
我认为CORS策略并非问题根源,因为之前使用Google Apps Script Web应用时,只要脚本出错就会出现相同的CORS拦截报错。
相关代码
Expo Web的POST请求代码
const res = await fetch(process.env.EXPO_PUBLIC_SHEETSURL, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ 'firstname': firstname, 'lastname': lastname, }) }) const output = await res.text() console.log(output)
Google Apps Script代码
function doPost(e) { return ContentService.createTextOutput("Hello World!") }
内容的提问来源于stack exchange,提问作者Brian Buslon
相关产品推荐
相关产品推荐

