【问题标题】:Getting an error when trying to read json file via fetch尝试通过 fetch 读取 json 文件时出错
【发布时间】:2018-11-08 12:20:15
【问题描述】:

我正在尝试从本地读取 json 文件,但作为响应,我收到了如下所示的错误

Fetch API cannot load file:///var/www/desktop-electron//dashboard/bnb.json. URL scheme "file" is not supported

这是我的代码

fetch(`${drr}/dashboard/bnb.json`)
            .then(res => res.json())
            .then(res => {
                this.currency = res;

                Object.assign(this.stock, res.pairs[0]);
                Object.assign(this.header, res.pairs[0]);
                this.header.name = res.name;
            }).catch(function(e) { console.log(e) });

我已经阅读了谷歌的所有解决方案,但无法解决问题!

【问题讨论】:

    标签: json file vue.js electron


    【解决方案1】:

    您是否尝试过完全省略“file://”方案? 还首先尝试将 URI 包装在请求对象中,如所述 here

    fetch(new Request(URI))
    

    【讨论】:

      【解决方案2】:

      Native fetch (Chromium) 不支持通过文件协议从本地加载资源。简而言之,您不能,您需要使用其他方式(如 fs.readfile 或其他方式)或将 json 托管到远程端点以使用 fetch。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-22
        • 1970-01-01
        • 2022-01-15
        • 2020-08-27
        • 2013-04-15
        • 2021-03-15
        • 2020-02-07
        相关资源
        最近更新 更多