javascript - 通过 fetch(URL) 从谷歌表中获取数据但被 CORS 策略阻止
问题描述
我正在尝试从 Google Sheet 接收数据并且它正在工作,但目前获取失败并出现以下错误。
const url = `https://docs.google.com/spreadsheets/d/e/${id}/pub?gid=${gid}&single=true&output=csv`;
const result = await fetch(url);
const csvText = await result.text();
解决方案
您可以使用代理服务器解决 CORS 问题。在这种情况下,您只需将 API 请求 URL 附加到代理服务提供商,例如,https://cors-anywhere.herokuapp.com/
var url = https://docs.google.com/spreadsheets/d/e/${id}/pub?gid=${gid}&single=true&output=csv;
var proxyUrl = https://cors-anywhere.herokuapp.com/${url};
fetch(proxyUrl)... //Make a request with this proxy url to navigate CORS issue
推荐阅读
- arrays - Excel 计算行中两个词的出现次数
- javascript - 汇总:从 Lodash 导入功能时“导入不存在的导出”
- python - 如何使用 PYQT5 在 QTreeView 中选择和编辑新创建的文件夹
- c++ - std::atomic 与非原子变量的性能是什么?
- javascript - Laravel 无法从 Vue-multiselect 获取值
- python - 指定轴时,Numpy 计算不同的标准偏差
- ios - 将 Reactive Cocoa 代码转换为 RxSwift?
- firebase - 在 Firestore 文档中添加时间戳
- javascript - 如何获取被点击的按钮的 id?反应JS
- asp.net-mvc - 需要使用 MVC 从 URL 中取出两个字符串。这是怎么做到的?