【发布时间】:2021-04-28 17:07:24
【问题描述】:
我正在尝试使用 PapaParse 读取 CSV 文件,但遇到 CORS 策略错误:
index.html:1 Access to XMLHttpRequest at 'file:///Users/macbook/Downloads/hh/csv/Patient_Experience.csv' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
我正在使用以下代码片段
var myurl='csv/Patient_Experience.csv';
Papa.parse(myurl, {
header: true,
download: true,
dynamicTyping: true,
complete: function(results) {
console.log(results);
}
});
此外,文件存在于 csv 目录中,如 csv/Patient_Experience.csv 我试图将 csv 文件放在 s3 中并在那里允许 cors 政策,但仍然面临同样的问题。
注意:我没有使用任何节点或任何其他库。其简单的纯 HTML、JS 流。
【问题讨论】:
-
Papa.parse 正在尝试通过 XMLHttpRequest 加载文件,这在
file:///环境中不起作用。您需要使用网络服务器并通过http://localhost...访问您的页面 -
如果我想在媒体播放器中添加这些文件。如果我尝试添加媒体播放器,它会起作用。我的问题是,基于媒体播放器 Linux 的播放器,所以如果它在媒体播放器上运行,那么它也应该在其他环境中运行。
标签: javascript jquery csv papaparse