【问题标题】:CORS Error, when try to read CSV file in the same directory using PapaParse [duplicate]CORS 错误,当尝试使用 PapaParse 读取同一目录中的 CSV 文件时 [重复]
【发布时间】: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


【解决方案1】:

我解决了以下问题。

  1. 在终端中安装 http-server。
  2. 再次运行此命令。
  3. http-server your_project_directory -p 3000
  4. your_project_directory,这将包含 index.html,其中包含脚本(使用 papaParse 从 CSV 读取)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 2020-07-29
    • 2019-05-19
    • 2018-06-27
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    相关资源
    最近更新 更多